jQuery Selectors
jQuery selectors are used to find the elements in the HTML page.
Here are some of the most frequently used selectors.
Finding all elements with specific tag name
To select all elements with specific tag, you can use below syntax.
$(“tag_name”)
For example – to select all elements with table tag, you can use below syntax.
$(“table”)
Below selector will select all <div> elements and <span> elements
$(“div,span”)
Below selector will select all <span> elements within <div> elements
$(“div span”)
Below selector will select all <span> elements and their parent is <div> element.
$(“div > span”)
Below selector will select all <span> elements if it comes right after div element. (similar to first sibling)
$(“div + span”)
Below selector will select all <span> elements followed by div element.
$(“div ~ span”)
Finding all elements with specific id
To select all element with specific id attribute value, you can use below syntax.
$(“#Value_Of_Id_Attribute”)
For example – If you want to select the element with Id attribute’s value as transactionNumber, you can use below syntax.
$(“#transactionNumber”)
Finding all elements with specific class name
To select all element with specific class attribute value, you can use below syntax.
$(“.className”)
Below selector will select all div elements that have call name as inactive
$(“div.inactive”)
Finding all elements
To select all elements, you can use below syntax.
$(“*”)
Selecting current element
To select current element, you can use below syntax.
$(this)
Finding all elements with specific tag name and attribute value
To find all input tag element having attribute type = ‘hidden’
input[type=’hidden’]
Find all input tag element having attribute type = ‘hidden’ and name attribute = ‘ren’
input[type=’hidden’][name=’ren’]
Find all input tag element with attribute type containing ‘hid’
input[type*=’hid’]
Find all input tag element with attribute type starting with ‘hid’
input[type^=’hid’]
Find all input tag element with attribute type ending with ‘den’
input[type$=’den’]
Below selector will select all elements that have src attribute.
$(“[src]”)
Below selector will selects all <img> elements where src attribute is not equal to xyz.
$(“img[src!=’xyz’]”)
Below selector will selects all <img> elements where src attribute starts with xyz.
$(“img[src|=’xyz’]”)
More complex Selectors
To select all even and odd tr elements, use below selector
- $(“tr:even”) – This will select all even rows from the table.
- $(“tr:odd”) – This will select all odd rows from the table.
- $(“a:active”) – This selector will select all active links.
- $(“a:hover”) – This selector will select link on which mouse is hovered.
- $(“a:visited”) – This selector will select all visited links.
- $(“a:link”) – This selector will select unvisited links.
- $(“:checked”) – This selector will select all checked elements.
- $(“:disabled”) – This selector will select all disabled elements.
- $(“:enabled”) – This selector will select all enabled elements.
- $(“:empty”) – This selector will select all elements that do not have any children.
- $(“:read-only”) – This selector will select all elements that have readonly attribute.
- $(“:read-write”) – This selector will select all elements that do not have readonly attribute.
- $(“:required”) – This selector will select all elements that have required attribute.
- $(“:optional”) – This selector will select all elements that do not have required attribute.
- $(“:out-of-range”) – This selector will select all elements that have out of range values.
- $(“:in-range”) – This selector will select all elements that have values in the range.
- $(“:root”) – This selector will select root element
- $(“:valid”) – This selector will select all valid elements
- $(“:invalid”) – This selector will select all invalid elements
- $(“input:focus”) – This selects the focused input element
Selecting children, siblings and parents
- $(“:first-child”) – This selects the first child of every element
Recent Comments