Created
August 14, 2019 02:24
-
-
Save darkylmnx/4d4fe3d742c22b6ca0f380b9d779d1e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// definition | |
function $(s) { return new lib(s); } | |
function lib(s) { this.$els = document.querySelectorAll(s); } | |
lib.prototype.css = function(prop, val) { | |
this.$els.forEach($el => ($el.style[prop] = val)); | |
// this is what makes it chainable | |
return this; | |
}; | |
lib.prototype.text = function(val) { | |
if (val) { | |
this.$els.forEach($el => ($el.textContent = val)); | |
} else { | |
return Array.from(this.$els) | |
.map($el => $el.textContent) | |
.join(' '); | |
} | |
// this is what makes it chainable | |
return this; | |
}; | |
// usage | |
const texts = $('a').text(); | |
console.log(texts); | |
$('a').css('color', 'red').text('this').text('no that'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment