rmurphey Adventures in JavaScript

More on jQuery selectors

7 Dec 2007 edit

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 does a nice job of demonstrating it pretty plainly, and it's a good reminder that, if I'd wanted to stick with my attribute-based selection, I could have helped my code a little bit by telling it where I expected to find those attributes so it wouldn't have had to look through every element on a ridiculously huge page.

{% codeblock lang:javascript %} $('#data_entry input, #data_entry select').filter('[name=value]'); {% endcodeblock %}