Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.