Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  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');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement