Skip to content

Instantly share code, notes, and snippets.

@darkylmnx
Created August 14, 2019 02:24
Show Gist options
  • Save darkylmnx/4d4fe3d742c22b6ca0f380b9d779d1e5 to your computer and use it in GitHub Desktop.
Save darkylmnx/4d4fe3d742c22b6ca0f380b9d779d1e5 to your computer and use it in GitHub Desktop.
// 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