#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
jQuery

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

SelectorSelects
$('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.

Explore Course