jQuery - Selectors

jQuery - Selectors

The jQuery library uses the power of Cascading Style Sheets (CSS) selectors to allow us to quickly and easily access elements or groups of elements in the Document Object Model (DOM).

The jQuery Selector is a function that uses expressions to find matching elements from the DOM based on given criteria. Simply put, selectors are used to select one or more HTML elements using jQuery. Once an element is selected, we can perform various operations on that selected element.

$factory() function

jQuery selectors start with a dollar sign and parentheses - $ () . The $() factory function uses the following three building blocks when selecting elements in a given document:

Sr.No. Selector and Description
one

Tag name

Represents a tag name available in the DOM. For example, $('p') selects all <p> paragraphs in the document.

2

Tag ID

Represents the tag available with the given ID in the DOM. For example, $('#some-id') selects the only element in the document that has the id some-id.

3

Tag Class

Represents a tag available for this class in the DOM. For example, $('.some-class') selects all elements in the document that have the class some-class.

Tag name

Represents a tag name available in the DOM. For example, $('p') selects all <p> paragraphs in the document.

Tag ID

Represents the tag available with the given ID in the DOM. For example, $('#some-id') selects the only element in the document that has the id some-id.

Tag Class

Represents a tag available for this class in the DOM. For example, $('.some-class') selects all elements in the document that have the class some-class.

All of the above items can be used on their own or in combination with other selectors. All jQuery selectors are based on the same principle, except for some tweaks.

NOTE. - Factory function $() is synonymous with jQuery() function . So if you're using any other JavaScript library where $ sign conflicts with something else, you can replace $ sign with the jQuery name and use the jQuery() function instead of $() .

How to use selectors?

Selectors are very useful and will be needed at every step when using jQuery. They get exactly the element you want from your HTML document.

The following table lists a few basic selectors and explains them with examples.

Sr.No. Selector and Description
one title

Selects all elements that match the given Name element .

2 #I'D

Selects one element that matches the given id .

3 .Class

Selects all elements that match the given .

4 Universal (*)

Selects all elements available in the DOM.

five Multiple elements E, F, G

Selects the combined results of all the specified selectors E, F, or G.

Selects all elements that match the given Name element .

Selects one element that matches the given id .

Selects all elements that match the given .

Selects all elements available in the DOM.

Selects the combined results of all the specified selectors E, F, or G.

Selector examples

Similar to the above syntax and examples, the following examples will help you understand how to use other types of other useful selectors −

Sr.No. Selector and Description
one

$('*')

This selector selects all elements in the document.

2

$("p>*")

This selector selects all elements that are children of the paragraph element.

3

$("#Special Identity")

This selector function gets the element with id = "specialID".

4

$("specialClass")

This selector gets all elements that have the class specialClass .

five

$("whether: no(.MyClass)")

Selects all elements matched by <li> that do not have class="myclass".

6

$("#specialID.specialClass")

This selector matches references with specialID and class specialClass .

7

$("pa.specialClass")

This selector matches links against the specialClass declared on <p> elements.

8

$("ul li: first")

This selector only gets the first <li> element of the <ul>.

nine

$("# container p")

Selects all elements matched by <p> that are children of the element with container id .

10

$("li > ul")

Selects all elements matched by <ul> that are children of the element matched by <li>

eleven

$("strong + them")

Selects all elements matching <em> that immediately follow the parent element matching <strong>.

12

$("p~ul")

Selects all elements that match a <ul> that follow a sibling element that matches a <p>.

13

$("code, them, strong")

Selects all elements matching <code> or <em> or <strong>.

fourteen

$("strong, .myclass")

Selects all elements matched by <strong> that are children of the element matched by <p> , as well as all elements that have the class myclass .

15

$(":blank")

Selects all elements that do not have child elements.

16

$("r: empty")

Selects all elements matched by <p> that have no child elements.

17

$("div[r]")

Selects all elements that match the <div> element that contain the element that matches the <p> element.

eighteen

$("p[.MyClass]")

Selects all elements matched by <p> that contain an element with class myclass .

19

$("a[@rel]")

Selects all elements matched by <a> that have the rel attribute.

twenty

$("input[@name=myname]")

Selects all elements matching <input> that have a name value exactly equal to myname.

21

$("input[@name=MyName]")

Selects all elements matched by <input> that have a name value that starts with myname .

22

$("a[@$rel=self]")

Selects all elements matched by <a> that have a rel attribute value ending in self .

23

$("a[@HREF=domain.com]")

Selects all elements matched by <a> that have an href value containing domain.com.

24

$("whether: even")

Selects all elements matched by <li> that have an even index value.

25

$("tr:odd")

Selects all elements matched by <tr> that have an odd index value.

26

$("li: first")

Selects the first <li> element.

27

$("li: last")

Selects the last <li> element.

28

$("li: visible")

Selects all elements matched by <li> that are visible.

29

$("li: hidden")

Selects all elements matching <li> that are hidden.

thirty

$(":radio")

Selects all radio buttons in the form.

31

$(":checked")

Selects all checkboxes in the form.

32

$(":input")

Selects only form elements (input, select, text field, button).

33

$(":text")

Selects only text elements (input [type = text]).

34

$("li:eq(2)")

Selects the third <li> element.

35

$("li:eq(4)")

Selects the fifth <li> element.

36

$("li:l(2)")

Selects all elements that match the <li> element before the third; in other words, the first two <li> elements.

37

$("p:l(3)")

selects all elements matching <p> elements before the fourth one; in other words, the first three <p> elements.

38

$("Li:GT(1)")

Selects all elements matching <li> after the second one.

39

$("p:gt(2)")

Selects all elements matching <p> after the third.

40

$("div/r")

Selects all elements that match the <p> element that are children of the element that matches the <div> element.

41

$("div //code")

Selects all elements matched by <code> that are children of the element matched by <div>.

42

$("//p//a")

Selects all elements matched by <a> that are children of the element matched by <p>

43

$("li: first-child")

Selects all elements matched by <li> that are the first children of their parent.

44

$("li: last child")

Selects all elements matched by <li> that are the last children of their parent.

45

$(":parent")

Selects all elements that are the parent of another element, including text.

46

$("li: contains (second)")

Selects all elements matched by <li> that contain the second text.

$('*')

This selector selects all elements in the document.

$("p>*")

This selector selects all elements that are children of the paragraph element.

$("#Special Identity")

This selector function gets the element with id = "specialID".

$("specialClass")

This selector gets all elements that have the class specialClass .

$("whether: no(.MyClass)")

Selects all elements matched by <li> that do not have class="myclass".

$("#specialID.specialClass")

This selector matches references with specialID and class specialClass .

$("pa.specialClass")

This selector matches links against the specialClass declared on <p> elements.

$("ul li: first")

This selector only gets the first <li> element of the <ul>.

$("# container p")

Selects all elements matched by <p> that are children of the element with container id .

$("li > ul")

Selects all elements matched by <ul> that are children of the element matched by <li>

$("strong + them")

Selects all elements matching <em> that immediately follow the parent element matching <strong>.

$("p~ul")

Selects all elements that match a <ul> that follow a sibling element that matches a <p>.

$("code, them, strong")

Selects all elements matching <code> or <em> or <strong>.

$("strong, .myclass")

Selects all elements matched by <strong> that are children of the element matched by <p> , as well as all elements that have the class myclass .

$(":blank")

Selects all elements that do not have child elements.

$("r: empty")

Selects all elements matched by <p> that have no child elements.

$("div[r]")

Selects all elements that match the <div> element that contain the element that matches the <p> element.

$("p[.MyClass]")

Selects all elements matched by <p> that contain an element with class myclass .

$("a[@rel]")

Selects all elements matched by <a> that have the rel attribute.

$("input[@name=myname]")

Selects all elements matching <input> that have a name value exactly equal to myname.

$("input[@name=MyName]")

Selects all elements matched by <input> that have a name value that starts with myname .

$("a[@$rel=self]")

Selects all elements matched by <a> that have a rel attribute value ending in self .

$("a[@HREF=domain.com]")

Selects all elements matched by <a> that have an href value containing domain.com.

$("whether: even")

Selects all elements matched by <li> that have an even index value.

$("tr:odd")

Selects all elements matched by <tr> that have an odd index value.

$("li: first")

Selects the first <li> element.

$("li: last")

Selects the last <li> element.

$("li: visible")

Selects all elements matched by <li> that are visible.

$("li: hidden")

Selects all elements matching <li> that are hidden.

$(":radio")

Selects all radio buttons in the form.

$(":checked")

Selects all checkboxes in the form.

$(":input")

Selects only form elements (input, select, text field, button).

$(":text")

Selects only text elements (input [type = text]).

$("li:eq(2)")

Selects the third <li> element.

$("li:eq(4)")

Selects the fifth <li> element.

$("li:l(2)")

Selects all elements that match the <li> element before the third; in other words, the first two <li> elements.

$("p:l(3)")

selects all elements matching <p> elements before the fourth one; in other words, the first three <p> elements.

$("Li:GT(1)")

Selects all elements matching <li> after the second one.

$("p:gt(2)")

Selects all elements matching <p> after the third.

$("div/r")

Selects all elements that match the <p> element that are children of the element that matches the <div> element.

$("div //code")

Selects all elements matched by <code> that are children of the element matched by <div>.

$("//p//a")

Selects all elements matched by <a> that are children of the element matched by <p>

$("li: first-child")

Selects all elements matched by <li> that are the first children of their parent.

$("li: last child")

Selects all elements matched by <li> that are the last children of their parent.

$(":parent")

Selects all elements that are the parent of another element, including text.

$("li: contains (second)")

Selects all elements matched by <li> that contain the second text.

You can use all of the above selectors with any HTML/XML element in general. For example, if the $("li: first") selector works on the <li> element, then $("p: first") will also work on the <p> element.