Skip to content

Instantly share code, notes, and snippets.

@BenBals
Last active August 29, 2015 14:14
Show Gist options
  • Save BenBals/1c519cfffcc123988f92 to your computer and use it in GitHub Desktop.
Save BenBals/1c519cfffcc123988f92 to your computer and use it in GitHub Desktop.
x - Small jQuery like Thing that can only add and remove Classes 1346 Bytes (yes Bytes)
var x;x=function(e){var n,s;return s={},n=e,s.element=function(){return document.querySelector(n)},s.elements=function(){return document.querySelectorAll(n)},s.e=s.element,s.es=s.elements,s.addClass=function(e,t){var l,o,a,r,i;if(void 0===t)for(a=s.elements(),r=0,i=a.length;i>r;r++)l=a[r],l.className===l.className.split(e).join("")&&(""===l.className?l.className=e:l.className+=" "+e);else{if(t>=s.elements().length||0>t)return console.warn("You provided an index ("+t+") that is out of range to a x.addClass. The selector was "+n),null;o=s.elements()[t],""===o.className?o.className=e:o.className+=" "+e}return e},s.append=function(e,t){var l,o,a,r,i;if(void 0===t)for(a=s.es(),r=0,i=a.length;i>r;r++)l=a[r],l.innerHTML+=e;else{if(t>=s.elements().length||0>t)return console.warn("You provided an index ("+t+") that is out of range to a x.append. The selector was "+n),null;o=s.es()[t],o.innerHTML+=e}return e},s.html=function(e,t){var l,o,a,r,i;if(void 0!==e&&"number"!=typeof e){if(void 0===t)for(a=s.es(),r=0,i=a.length;i>r;r++)l=a[r],l.innerHTML=e;else{if(t>=s.elements().length||0>t)return console.warn("You provided an index ("+t+") that is out of range to a x.html. The selector was "+n),null;o=s.es()[t],o.innerHTML=e}return e}return void 0===e?s.e().innerHTML:e>=s.elements().length||0>e?(console.warn("You provided an index ("+e+") that is out of range to a x.html. The selector was "+n),null):s.es()[e].innerHTML},s.removeAllClasses=function(e){var t,l,o,a,r;if(void 0===e)for(o=s.elements(),a=0,r=o.length;r>a;a++)t=o[a],t.className="";else{if(e>=s.elements().length||0>e)return console.warn("You provided an index ("+e+") that is out of range to a x.removeAllClasses. The selector was "+n),null;l=s.elements()[e],l.className=""}return""},s.removeClass=function(e,t){var l,o,a,r,i;if(void 0===t)for(a=s.elements(),r=0,i=a.length;i>r;r++)l=a[r],l.className=l.className.split(e).join("").split(" ").join(" ").trim();else{if(t>=s.elements().length||0>t)return console.warn("You provided an index ("+t+") that is out of range to a x.removeClass. The selector was "+n),null;o=s.elements()[t],o.className=o.className.split(e).join("").split(" ").join(" ").trim()}return e},s};
@BenBals
Copy link
Author

BenBals commented Feb 6, 2015

Syntax: x(selector).action()
Actions:

  • addClass(className)
  • append(newHtml)
  • css(key, value)
  • html(html)
  • removeClass(className)
  • removeAllClasses(className)

Note: you can also pass an index so only the first, second.. element is affected (e.g. x('.myClass').addClass('newClass', 0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment