Unit-2 Jquery Selectors
Unit-2 Jquery Selectors
jQuery Selectors are used to select and manipulate HTML elements. They are very important
part of jQuery library.
With jQuery selectors, you can find or select HTML elements based on their id, classes,
attributes, types and much more from a DOM.
In simple words, you can say that selectors are used to select one or more HTML elements
using jQuery and once the element is selected then you can perform various operation on that.
All jQuery selectors start with a dollor sign and parenthesis e.g. $(). It is known as the factory
function.
Every jQuery selector start with thiis sign $(). This sign is known as the factory function. It
uses the three basic building blocks while selecting an element in a given document.
Example: -
<!DOCTYPE html>
<html>
<head>
<title>First jQuery Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/
jquery.min.js">
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("p").css("background-color", "pink");
});
</script>
</head>
<body>
<p>This is first paragraph.</p>
<p>This is second paragraph.</p>
<p>This is third paragraph.</p>
</body>
</html>
The jQuery selectors can be used single or with the combination of other selectors. They are
required at every steps while using jQuery. They are used to select the exact element that you
want from your HTML document.
1) Name: It selects all elements that match with the given element name.
2) #ID: It selects a single element that matches with the given id.
3) .Class: It selects all elements that matches with the given class.
4) Universal(*) It selects all elements available in a DOM.
5) Multiple Elements It selects the combined results of all the specified selectors A,B
A,B,C and C.
:first-child $("p:first-child") It will select all p elements that are the first child
of their parent
:first-of-type $("p:first-of-type") It will select all p elements that are the first p
element of their parent
:last-child $("p:last-child") It will select all p elements that are the last child
of their parent
:last-of-type $("p:last-of-type") It will select all p elements that are the last p
element of their parent
:nth-child(n) $("p:nth-child(2)") This will select all p elements that are the 2nd
child of their parent
:nth-last-child(n) $("p:nth-last-child(2)") This will select all p elements that are the 2nd
child of their parent, counting from the last child
:nth-of-type(n) $("p:nth-of-type(2)") It will select all p elements that are the 2nd p
element of their parent
:nth-last-of- $("p:nth-last-of- This will select all p elements that are the 2nd p
type(n) type(2)") element of their parent, counting from the last
child
:only-child $("p:only-child") It will select all p elements that are the only child
of their parent
:only-of-type $("p:only-of-type") It will select all p elements that are the only
child, of its type, of their parent
parent > child $("div > p") It will select all p elements that are a direct child
of a div element
parent descendant $("div p") It will select all p elements that are descendants
of a div element
element + next $("div + p") It selects the p element that are next to each div
elements
element ~ siblings $("div ~ p") It selects all p elements that are siblings of a div
element
:eq(index) $("ul li:eq(3)") It will select the fourth element in a list (index
starts at 0)
:gt(no) $("ul li:gt(3)") Select the list elements with an index greater
than 3
:lt(no) $("ul li:lt(3)") Select the list elements with an index less than 3
:not(selector) $("input:not(:empty)") Select all input elements that are not empty
OR
Element Selector:
Syntax: $("element")
Targets all HTML elements of the specified type.
Example: $("p") selects all paragraphs on the page.
ID Selector:
Syntax: $("#id")
Targets the element with the specified ID attribute.
Example: $("#myElement") selects the element with the ID "myElement".
Class Selector:
Syntax: $(".class")
Targets all elements with the specified class.
Example: $(".myClass") selects all elements with the class "myClass".
Attribute Selector:
Syntax: $("[attribute='value']")
Targets elements with a specific attribute and value.
Example: $("[data-type='image']") selects elements with the attribute data-type set to
"image".
Descendant Selector:
Syntax: $("ancestor descendant")
Targets all descendants of the specified ancestor element.
Example: $("ul li") selects all <li> elements that are descendants of a <ul>.
Child Selector:
Syntax: $("parent > child")
Targets all direct children of the specified parent element.
Example: $("ul > li") selects all <li> elements that are direct children of a <ul>.
Sibling Selector:
Syntax: $("prev + next")
Targets the next sibling element immediately following the specified previous sibling.
Example: $("h2 + p") selects the <p> element that directly follows an <h2>.
:last Selector:
Syntax: :last
Targets the last element in a selection.
Example: $("p:last") selects the last <p> element on the page.