SHARE
TWEET

Untitled

a guest Aug 25th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // definition
  2. function $(s) { return new lib(s); }
  3.  
  4. function lib(s) { this.$els = document.querySelectorAll(s); }
  5.  
  6. lib.prototype.css = function(prop, val) {
  7.     this.$els.forEach($el => ($el.style[prop] = val));
  8.     // this is what makes it chainable
  9.     return this;
  10. };
  11.  
  12. lib.prototype.text = function(val) {
  13.     if (val) {
  14.         this.$els.forEach($el => ($el.textContent = val));
  15.     } else {
  16.         return Array.from(this.$els)
  17.             .map($el => $el.textContent)
  18.             .join(' ');
  19.     }
  20.  
  21.     // this is what makes it chainable
  22.     return this;
  23. };
  24.  
  25. // usage
  26. const texts = $('a').text();
  27. console.log(texts);
  28.  
  29. $('a').css('color', 'red').text('this').text('no that');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top