Comprehensive Guide to jQuery Selectors
Learn to use jQuery selectors to find and manipulate DOM elements efficiently. Explore basic selectors, combinators, and advanced techniques for precise element targeting.
jQuery Selectors
jQuery selectors allow you to find and manipulate DOM elements. Here’s a detailed guide:
Basic Selectors
*
: Matches all elements.element
: Matches all elements of the specified type (e.g.,p
,div
,span
).#id
: Matches an element with the specified ID..class
: Matches elements with the specified class.[attribute]
: Matches elements with the specified attribute.[attribute=value]
: Matches elements with the specified attribute and value.
Combinators
>
: Matches elements that are direct children of another element.Syntax
$('div > p') // Selects all
elements that are direct children of
elementsSyntax
$('div p') // Selects all
elements that are descendants of
elements+
: Matches elements that are the next sibling of another element.Syntax
$('#element1 + p') // Selects the first
element that comes after #element1
~
: Matches elements that are siblings of another element.Syntax
$('#element1 ~ p') // Selects all
elements that are siblings of #element1
Pseudo-classes
:first
: Matches the first element in a set of matched elements.:last
: Matches the last element in a set of matched elements.:eq(index)
: Matches the element at the specified index.:even
: Matches all even elements in a set of matched elements.:odd
: Matches all odd elements in a set of matched elements.:hidden
: Matches all hidden elements.:visible
: Matches all visible elements.
Examples
HTML
Syntax
<div id="container"> <p class="first">First paragraph</p> <div class="inner"> <p class="second">Second paragraph</p> </div> <p class="third">Third paragraph</p> </div>
JavaScript
Syntax
// Select the second paragraph within the container $('#container > div > p'); // Select all elements with the class "first" or "second" $('p.first, p.second'); // Select the third paragraph using :eq() $('p:eq(2)');
jQuery Selector Syntax
The jQuery selector enables you to find DOM elements on your web page. It uses CSS selector patterns as well as its own patterns to match elements. The syntax is:
Syntax
$(selector, context);
The
selector
parameter specifies a pattern to match the elements, andcontext
(optional) specifies where to start searching within the DOM hierarchy.Common Selectors
Select Elements by Name
Syntax
$('p').append('This is paragraph.'); // Appends text to all p elements $('div').append('This is div.'); // Appends text to all div elements
Select Elements by ID
Syntax
$('#impPrg').append('This element\'s id is "impPrg"'); $('#myDiv2').append('This element\'s id is "myDiv2"');
Select Elements by Attribute
Syntax
$('[class]').append('This element has class attribute'); $('[class="yellowDiv"]').append('This element includes class="yellowDiv" attribute');
Selector Patterns
Category Selector Description Find element $('div')
Find all <div>
elementsFind descendant elements $('div p')
Find all <p>
elements which are descendants of<div>
Find by ID $('#myDiv')
Find element whose ID is myDiv
Find by CSS class $('.myCSSClass')
Find all elements with class myCSSClass
Find by attribute $('[class]')
Find all elements with the class attribute (regardless of value) Find by input type $(':button')
Selects all input elements where type="button" Additional Tips
- Use browser developer tools to inspect elements and experiment with selectors.
- Combine selectors for more specific targeting.
- Consider performance implications when using complex selectors.
By mastering these selectors, you can efficiently manipulate DOM elements and create dynamic web applications using jQuery.