JQuery selectors allow you to select and manipulate HTML elements as a group or as a single element.
Selectors are an essential part of jQuery that provide a great way to locate nodes in the DOM quickly and easily.
With jQuery selectors you can find elements based on their id, classes, types, attributes, values of attributes and much more
All type of selectors in jQuery, start with the dollar sign and parentheses: $().
Element :
An element refers to a name of a tag. A <p>, <p id="head">, <p class="show"> can be an element. The following selector can be used to find the elements.
$('#myid') - selects element with id = "myid" $('.myclass') - selects element with class = "myclass" $(this) - selects current HTML element $("*") - selects all elements
Element Selectors
The jQuery element selector selects elements based on their tag names.
You can select paragraph elements on a page like this:
example:
div having id=d1 Click
div having class=box Click
Demo:
paragraph having id=p1 Click
div having id=d1 Click
div having class=box Click
Attribute Selectors
The attribute selector selects each element having a specific attribute name,or having a specified attribute with a certain value,or having a specified attribute and it ends with a certain value.
,or having a specified attribute and it starts with a certain value
The * selector selects every single element in the document, including html, head and body.It can be used to select all child elements within the specified element.
Note : Depending on use, the * selector can be slow, and heavy for some browsers to process.
Example:
DIV
SPAN
Paragraph
Demo:
DIV
SPAN
Paragraph
Select this
It selects current HTML element.
Example:
Demo:
Child Selector
The ("parent > child") selector selects all elements that are a direct child of the specified element.
The :eq() selector selects an element with a specific index number.
The index numbers start at 0, so the first element will have the index number 0 (not 1).
This is mostly used together with another selector to select a specifically indexed element in a group .
example:
My name is Donald. first paragraph
I live in Duckburg.second paragraph
My best friend is Mickey.third paragraph
Demo:
My name is Donald. first paragraph
I live in Duckburg.second paragraph
My best friend is Mickey.third paragraph
Question for jquery selectors
How do you select an element using class or ID ?
Ans : $('#divid')-> It selects the element having id="divid" $('.cls')-> It selects the element having class ="cls"
How do you select an element having src value ending with jpg ?
Ans : $("[src$='jpg']")-> It selects the element having attribute src ending with jpg
How do you select children ?
Ans : $('parent > child') -> It selects the "child" element having parent element="parent" $('.cls')-> It selects the element having class ="cls"
What are the ways for selecting the element ?
Ans : We can find elements based on their id, classes, types, attributes, values of attributes
How to count all element with in a div having id="id_dv"?
Ans : $("#id_dv").find("*").length ->It gives the length of element inside the div
How to count all element having border="3px solid red" with in a window ?
Ans : var cnt = $("[style='border: 3px solid red;']").length; ->It gives the length of element inside the page
How to change background color of a third div element from a list of five div ?
Ans : $("div:eq(2)").css("background-color","yellow");
How to select all div elements that have a p element ?
Ans : $("div:has(p)") -> It selects the div having a paragraph
How to select all input elements that are not empty ?
Ans :
$(":input").each(function() {
if($(this).val() != "")
alert("Empty Fields!!");
}); OR
for a more specific answer, if you only want those types, change the selector like this: