Jquery Selectors
Written By: Avinash Malhotra
Updated on
Jquery Selectors
Jquery use css like selectors. But Unlike css, jquery is having many more selectors than CSS2. Here is a list of complete jquery selectors with live example and code.
Jquery Basic Selectors
Jquery | Code | Description | Execute | Output |
---|---|---|---|---|
$("element") |
$("p").css({ "color":"red"}) |
select all elements with tag name in example. | Para 1 Para 2 |
|
$("#id") |
$("#p1").css({ "color":"blue"}) |
select single element with id name in example. If id name is repeated, only first match will be executed. |
Para with id p1 |
|
$(".class") |
$(".p1").css({ "color":"aqua"}) |
select all elements with class name in example. | Para with class p1 another p with class p1 |
|
$("ul li") |
$("ul li").css({ "color":"yellow"}) |
select all descendent of ul element. |
|
|
$("ul > li") |
$("ul > li").css({ "color":"aqua"}) |
select all child selectors of ul element. |
|
|
$("ul,ol") |
$("ul,ol").css({ "color":"gray"}) |
select grouping of ul and ol element. |
|
Jquery Basic Filters
Jquery | Code | Description | Execute | Output |
---|---|---|---|---|
$(":first") |
$("ul li:first").css({ |
select the first match elementin example. |
|
|
$(":last") |
$("ul li:last").css({ |
select the last match elementin example. |
|
|
$(":not(selector)") |
$("ul li:not(li:first)").css({ |
select all elements except givenin example. |
|
|
$(":even") |
$("ul li:even").css({ |
select all even indexed elementsin example. |
|
|
$(":odd") |
$("ul li:odd").css({ |
select all odd indexed elements in example. |
|
|
$(":eq(index)") |
$("ul li:eq(1)").css({ |
select element atnth index in example. |
|
|
$(":lt(index)") |
$("ul li:lt(2)").css({ "background":"red"}) |
select elements greater than nth index in example. |
|
|
$(":gt(index)") |
$("ul li:gt(2)").css({ "background":"red"}) |
select elements greater than nth index in example. |
|
Filter by Content
Jquery | Code | Description | Execute | Output | ||||
---|---|---|---|---|---|---|---|---|
$(":contains(text)") |
$("p:contains(hello)").css({ |
select the p element with text "hello"in example. | hola hello |
|||||
$(":empty") |
$("ul li:empty").css({ |
select only empty elements in example. |
|
|||||
$(":has(selector)") |
$("ul li:has(a)").css({ |
select only elements with descendants a in example. |
|
|||||
$(":parent") |
$("table tr td:parent").css({ |
:parent is inverse of : empty in example. |
|
Child Filters
Jquery | Code | Description | Execute | Output |
---|---|---|---|---|
$(":first-child") |
$("ul li:first-child").css({ |
select first child element in example. |
|
|
$(":last-child") |
$("ul li:last-child").css({ |
select last child element in example. |
|
|
$(":nth-child(n)") |
$("ul li:nth-child(2)").css({ |
select nth child element in example. |
|
|
$(":nth-child(odd)") |
$("ul li:nth-child(odd)").css({ |
select odd child elements in example. |
|
|
$(":nth-child(even)") |
$("ul li:nth-child(even)").css({ |
select even child elements in example. |
|
|
$(":nth-child(equation)") |
$("ul li:nth-child(3n)").css({ |
select nth factor elements in example. |
|
|
$(":only-child") |
$("ul li:only-child").css({ |
select element with only one child in example. |
|
Attribute Filters
Jquery | Code | Description | Execute | Output |
---|---|---|---|---|
$("[ attribute ]") |
$("ul li[id]").css({ |
select element with attribue name in example. |
|
|
$("[ attribute='value' ]") |
$("input[ name='firstname']").css({ |
select element with attribute name and value in example. | ||
$("[ attribute!='value' ]") |
$("li[ class!='col']").css({ |
select element with attribute value not equal to value in example. |
|
|
$("[ attribute^='value' ]") |
$("li[class^='col']").css({ |
select element with attribute value starting exactly with given string in example. |
|
|
$("[ attribute$='value' ]") |
$("li[class$='2']").css({ |
select element with attribute value ending exactly with given string in example. |
|
|
$("[ attribute*='value' ]") |
$("li[class*='o']").css({ |
select element that have attribute with a value containing subtring in example. |
|