jQuery Selectors
jQuery selectors let you find elements on a page so you can read or change them, using the same syntax as CSS selectors plus a few extras. You pass a selector string to $() and get back a jQuery object holding every matching element. Almost everything in jQuery starts with a selector, whether you are hiding a box, adding a class or attaching a click handler.
The basic selectors
| Selector | Selects |
|---|---|
$('p') | All <p> elements. |
$('#header') | The element with id="header". |
$('.card') | All elements with class card. |
$('ul li') | All <li> inside a <ul>, at any depth. |
$('ul > li') | Only direct children. |
$('h2 + p') | A <p> immediately after an <h2>. |
$('a[target="_blank"]') | Links with a specific attribute value. |
$('input[name^="user"]') | Inputs whose name starts with "user". |
$('li:first-child'), $('tr:nth-child(odd)') | Standard CSS position selectors. |
$('h1, h2, h3') | Several selectors combined. |
Selectors in use
$(function () { // run after the DOM is ready
$('.card').addClass('shadow'); // every card
$('#notice').text('Saved!'); // one element by id
$('table.prices tr:nth-child(even)').css('background', '#f3f4f6');
// Form states
$('input:checked').closest('label').addClass('selected');
$('button:disabled').attr('title', 'Fill in all fields first');
// Scope a search to a container, then narrow further
const $menu = $('#menu');
$menu.find('a').on('click', function () {
$menu.find('a').removeClass('active');
$(this).addClass('active'); // the clicked link
});
// Filtering an existing selection
$('li').filter('.done').fadeTo(200, 0.5);
$('li').not('.done').first().css('font-weight', 'bold');
// Check whether anything matched
if ($('#promo').length === 0) {
console.log('No promo on this page');
}
});
jQuery-specific filters
jQuery adds selectors that are not part of CSS, such as :visible, :hidden, :checkbox and :contains("text"). They are convenient but slower, because the browser's fast built-in selector engine cannot handle them, so jQuery checks elements one by one. The jQuery documentation recommends selecting with plain CSS first and then narrowing with methods: write $('li').filter(':visible') rather than putting :visible in the main selector. Older position filters like :first, :eq() and :odd are deprecated in jQuery 3; use the methods .first(), .eq(index) or CSS :nth-child instead.
Working with the result
A jQuery object always behaves like a list, even with one element. Most methods, such as .addClass() or .hide(), apply to every element in it automatically, so you rarely need a loop. Methods that read a value, like .text() or .val(), return only the first element's value. To get the underlying DOM element, use $el[0] or $el.get(0). Many developers prefix variables holding jQuery objects with $, as in $menu, to make this clear.
Selector slips that waste time
The most common one is that the selector matches nothing, yet no error appears. jQuery silently returns an empty object, and calling methods on it does nothing. Check .length when something seems to be ignored. Usual causes are a missing # or ., a typo in a class name, or running the code before the elements exist; wrapping code in $(function () { ... }) waits until the DOM is ready.
Special characters cause another failure. An id like user.name or item[1] must be escaped, for example with $.escapeSelector('user.name'). Finally, avoid running the same selector over and over in a loop or event handler. Store it in a variable once, as $menu is above, and reuse it.
What to carry forward
Use CSS-style selectors with $(), scope searches with .find() inside a container, narrow selections with .filter(), .not() and .first(), and check .length when nothing happens. With elements selected, the next step is responding to user actions with jQuery events.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.