Skip to content

Instantly share code, notes, and snippets.

@jcgertig
Last active August 29, 2015 14:06
Show Gist options
  • Save jcgertig/daa56c711de5897029a3 to your computer and use it in GitHub Desktop.
Save jcgertig/daa56c711de5897029a3 to your computer and use it in GitHub Desktop.
Basic jQuery functions
Element.prototype.attr = function(name, value) {
if (typeof value === 'undefined') {
return this.getAttribute(name);
}
return this.setAttribute(name, value);
};
Element.prototype.val = function(value) {
if (typeof value === 'undefined') {
return this.value;
}
return (this.value = value);
};
Element.prototype.css = function(name, value) {
if (typeof value === 'undefined') {
if (typeof name === 'object') {
var css = name;
Object.keys(css).forEach(function(name){
this.style[name] = css[name];
}.bind(this));
return this.style;
}
return this.style[name];
}
return (this.style[name] = value);
};
Element.prototype.click = function(handler) {
return this.addEventListener('click', handler);
};
Element.prototype.on = function(event, handler) {
return this.addEventListener(event, handler);
};
var $ = function(selector) {
return document.querySelector(selector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment