Questions tagged [jquery-selectors]

Selectors can be used in jQuery to match a set of elements in a document. Most CSS selectors are implemented, as well as a set of custom ones.

The core includes a DOM element selections engine named which traverses the DOM. The current version of Sizzle is v2.3.3. Sizzle has influenced the architecture of other Javascript frameworks such as v3 and and motivated the creation of the standard .

A complete list of supported selectors is available at jQuery's API documentation: Selectors. Most CSS selectors are implemented, as well as a set of custom ones.

Examples of valid selectors include $(".className"), $("#idName"), and $("div.className:not('#idName')).

Optimizing jQuery

Cache

var that = $(this);
that.find("div");

Cache your selectors by storing it to a variable if your going to use the same selector more than once so you don't avoid repeating selectors. Doing this keeps jQuery from having to search the entire DOM every time you call the selector.

Use ID-based selectors

//slow
$( ".redbox" );

//better
$( "#container div.redbox" );

// Fast!
$( "#container" ).find( "div.redbox" );

.find() is faster because the first selection is handled without going through the Sizzle selector engine – ID-only selections are handled using document.getElementById(), which is extremely fast because it is native to the browser.

Specificity

// slow
$( "div.data .redrow" );

// Fast!
$( ".data td.redrow" );

Be specific on the right-hand side of your selector, and less specific on the left. Use tag.class if possible on your right-most selector, and just tag or just .class on the left.

A "flatter" DOM also helps improve selector performance, as the selector engine has fewer layers to traverse when looking for an element.

Universal Selector

// slow
$( ".robot > *" );
// better
$( ".robot" ).children();

// Implied universal selection. (slow!)
$( ".redform :radio" );
// Same thing, explicit now. (better)
$( ".redform *:radio" );
// Fast!
$( ".redform input:radio" );

Chaining

// slow
$( "#box" ).hide();
$( "#box" ).show();

// fast
var box = $( "#box" );
box.hide().show();

Instead of selecting the same thing multiple times for multiple methods, just have one selector with all the methods you needs chained onto it. Almost every jQuery method returns the object it was using. Doing this keeps searches and variable accesses to a minimum.

Direct descenders

// slow
$('body h1')
$('body').find('h1')

// faster
$('body > h1')
$('body').children('h1')

Use direct-descendant operators whenever possible.

HTML5

If you can, use HTML5. HTML5 creates new page layout elements like section, header, footer, article, etc.. Having more specific layout tags means there are less items that share a given tag name, which translates into better selectors.

Related tags

8575 questions
2854
votes
39 answers

How can I know which radio button is selected via jQuery?

I have two radio buttons and want to post the value of the selected one. How can I get the value with jQuery? I can get all of them like this: $("form :radio") How do I know which one is selected?
juan
  • 74,179
  • 49
  • 153
  • 188
2387
votes
34 answers

Get selected text from a drop-down list (select box) using jQuery

How can I get the selected text (not the selected value) from a drop-down list in jQuery?
haddar
  • 24,077
  • 3
  • 18
  • 12
2299
votes
19 answers

How to get the children of the $(this) selector?

I have a layout similar to this:
and would like to use a jQuery selector to select the child img inside the div on click. To get the div, I've got this selector: $(this) How can I get the child img using a…
Alex
  • 25,366
  • 5
  • 27
  • 36
2149
votes
14 answers

How can I select an element with multiple classes in jQuery?

I want to select all the elements that have the two classes a and b. So, only the elements that have both classes. When I use $(".a, .b") it gives me the union, but I want the intersection.
Mladen
  • 24,320
  • 11
  • 36
  • 47
1498
votes
19 answers

How can I get the ID of an element using jQuery?

Why doesn't the above work, and how should I do this?
fearofawhackplanet
  • 47,230
  • 49
  • 149
  • 249
1353
votes
14 answers

How can I select an element by name with jQuery?

I have a table column I’m trying to expand and hide. jQuery seems to hide the elements when I select it by class but not by the element’s name. For example: $(".bold").hide(); // Selecting by class works. $("tcol1").hide(); // Selecting by name…
T.T.T.
  • 29,703
  • 45
  • 120
  • 161
1262
votes
22 answers

jQuery get specific option tag text

All right, say I have this: What would the selector look like if I wanted to get "Option B" when I…
Paolo Bergantino
  • 449,396
  • 76
  • 509
  • 431
1073
votes
31 answers

Set select option 'selected', by value

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the value of the option that must be selected? I have the following HTML: