Use jQuery-like Selectors without jQuery.

If you wish to use jQuery-like selectors without having to actually load the jQuery framework, you can use this simple function to mimic selector behavior. This function will returns false if nothing found, a DOM node if exactly one element is found and the list of matches otherwise.

function $( cssquery ) {
    var t = document.querySelectorAll(cssquery);
    return (t.length === 0) ? false : (t.length === 1) ? t[0] : t;
}

Author: Erik Haubold
Tags: dom css

comments powered by Disqus