Avoid bare class selectors in jQuery

It just happened again: I was looking at someone else’s jQuery and I came across something like this:
$(’.button’).click(function() { /* do something */ });
This is a classic case of “just because you can, doesn’t mean you should.” This little bit of jQuery will, indeed, find every element on a page with a class of “button”, [...]

More on jQuery selectors

Following up on my 13 seconds of selection hell:
jQuery: what are the fastest selectors?
Turns out that, as you might expect, $(’div.class’) is faster than $(’.class’), and so is $(’div’).filter(’.class’). Knowing where not to look (”skip anything that’s not a div”) helps make jQuery faster. To which you might say, “well duh.” Regardless, the post above [...]

13 seconds: Attribute vs. ID selectors in jQuery

Before:
 
// select all elements with an id or name attribute of fieldName;
// some are inputs (name attribute), some are td’s (id attribute)
var $field = $(’#’ + fieldName + ‘, [name=' + fieldName + ']‘);
 
After:
 
// give inputs both a name and an id attribute,
// and then just select fields and td’s via id
var $field = $(’#’ [...]