Skip to content

Instantly share code, notes, and snippets.

@mmfilesi
Created March 22, 2016 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmfilesi/50de66744914f4a94d57 to your computer and use it in GitHub Desktop.
Save mmfilesi/50de66744914f4a94d57 to your computer and use it in GitHub Desktop.
jquery style
'use strict';
(function () {
var foo = function (params) {
return new DomModule(params);
};
var DomModule = function (params) {
var selector = document.querySelectorAll(params),
i = 0;
this.length = selector.length;
for (; i < this.length; i++) {
this[i] = selector[i];
}
return this;
};
var methods = {};
methods.addClass = function(classAdd) {
var len = this.length;
if ( classAdd ) {
while (len--) {
this[len].classList.add(classAdd);
}
}
return this;
};
methods.removeClass = function(classRemove) {
var len = this.length;
if ( classRemove ) {
while (len--) {
this[len].classList.remove(classRemove);
}
}
return this;
};
methods.toggleClass = function(classToggle) {
var len = this.length;
if (classToggle ) {
while (len--) {
this[len].classList.toggle(classToggle);
}
}
return this;
};
methods.setStyle = function(style, value) {
var len = this.length;
if ( style && value ) {
while (len--) {
this[len].style[style] = value;
}
}
return this;
};
methods.text = function(text) {
var len = this.length;
while (len--) {
this[len].textContent = text;
}
return this;
};
methods.html = function(content) {
var len = this.length;
if ( content.indexOf('script') !== -1 ) {
content = 'scripts are not allowed';
}
while (len--) {
this[len].innerHTML = content;
}
return this;
};
methods.remove = function() {
var len = this.length;
while (len--) {
if ( this[len].parentNode ) {
this[len].parentNode.removeChild(this[len]);
}
}
return true;
};
foo.fn = ArrakisDomModule.prototype = methods;
if ( !window.foo ) {
window.foo = foo;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment