jQuery selectors and attribute selectors are some of the best features jQuery has to offer when it comes to DOM manipulation. They allow you to quickly select all elements or groups of element of a given tag name, attribute name or their contents and allow you to manipulate them as a group or a single node. This article can be used as a beginners guide to selectors or as a cheahsheet or reference
The following table lists the different methods you have available to you to select nodes when using jQuery. All of the listed selectors should be wrapped in the following to stop your jQuery scripts conflicting with other libraries:
(function ($) {
//Your code here
})(jQuery);
jQuery selectors and examples
| Selector | Example | Description |
|---|---|---|
| List accurate as of jQuery 1.3 | ||
| * | $(‘*’); | This selector is a wild card method and will select all elements in a document. |
| #id | $(‘#id’); | This selector selects an element with the given ID. |
| .class | $(‘.class’) | The class selector will gather all elements in the document with the given class name |
| element | $(‘element’) | This selector will collect all elements in a document with the given tag name i.e. table, ul, li, a etc. |
| a, b, c. … n | $(‘th, td, .class, #id’) | This method can use multiple selection patterns to collect elements. |
| parent child | $(‘li a’) | This will select all “a” elements that are a descendant of “li” |
| a > b | $(‘table > tr’); | This will select all b elements which are a child element of a or in our example all tr elements in a table or tables. |
| a + b | $(‘li + a’); | This will select all “a” elements that are an immediate descendant of “li” in our example. |
| a ~ b | $(‘p ~ ul’); | This selector will select all “ul” elements that are a sibling of “p” |
| :first | $(‘ul li:first’); | Returns the first element in a result set |
| :first-child | $(‘ul li:first-child’); | Returns the first child element of the parent element. |
| :last | $(‘ul li:last’); | Returns the last element in a result set |
| :last-child | $(‘ul li:last-child’); | Returns the last child element of the parent element. |
| |
$(‘div p:only-child’); | Returns elements which are the only child of the parent element. |
| :not(a) | $(‘input:not(:checked)’); | Returns all elements that are not “a” on in our example all input elements that are not checked |
| :has(a) | $(‘div:has(p)’); | Returns all elements with a descendant that matches a in out example a “div” that contains a “p”. |
| |
$(‘ul li:odd’); | Returns all odd elements in a result set (zero based) |
| :even | $(‘ul li:even’); | Returns all even elements in a result set (zero based) |
| :eq(n) | $(‘ul li:eq(n)’); | Returns a numbered element identified by n (zero based) |
| :gt(n) | $(‘ul li:gt(n)’); | Returns all elements in a result set greater than n (zero based) |
| :lt(n) | $(‘ul li:lt(n)’); | Returns all elements in a result set less than n (zero based) |
| :nth-child(n) | $(‘ul li:nth-child(n)’); | Returns the nth child in a result set (one based) |
| :nth-child(odd) | $(‘ul li:nth-child(odd)’); | Returns all odd numbered elements in a result set (one based) |
| :nth-child(even) | $(‘ul li:nth-child(even)’); | Returns all even numbered elements in a result set (one based) |
| :nth-child(formula) | $(‘ul li:nth-child(3n)’); | Returns every nth child in a result set. In our example every third child (one based) |
| :header | $(‘:header’); | Returns all heading elements e.g. h1, h2, etc. |
| :animated | $(‘ul:animated’); | Returns elements with an animation currently in progress |
| :contains(text) | $(‘:contains(hello)’); | Returns all elements containing the passed string |
| :empty | $(‘:empty’); | Returns all elements that contain no child nodes |
| :parent | $(‘li:parent’); | Returns all elements that a parent nodes to any other DOM element including text nodes. |
| :hidden | $(‘ul:hidden’); | Returns all hidden elements that are hidden with CSS or input fields of the type “hidden” |
| :visible | $(‘ul:visible’); | Returns all visible elements |
| [attribute] | $(‘[href]‘); | Returns all elements that contain the passed attribute in our example any element with a “href” attribute |
| [attribute=value] | $(‘[rel=external]‘); | Returns all elements that the passed attribute value is equal to the passed value. In our example ant element with a “rel” attribute equal to “external” |
| ['attribute!=value'] | $(‘[rel!=external]‘); | Returns all elements that the passed attribute value is not equal to the passed value. In our example ant element with a “rel” attribute that is not equal to “external” |
| [attribute!=value] | $(‘[class^=open]‘); | Returns all elements that the passed attribute value start with the passed value. In our example any element thats “class” attribute value begins with “open” |
| [attribute$=value] | $(‘[id$=-wrapper]‘); | Returns all elements that the passed attribute value ends with the passed value. In our example any element whos “id” ends with “-wrapper” |
| [attribute*=value] | $(‘[class*=offer]‘); | Returns all elements that the passed attribute value contains the passed value. In our example any element whos “class” contains the string “offer” |
| :input | $(‘:input’); | Returns only input elements of the tag name input, select, textarea and button |
| :text | $(‘:text’); | Returns only input elements of the type “text” |
| :password | $(‘:password’); | Returns only input elements of the type “password” |
| :radio | $(‘:radio’); | Returns only input elements of the type “radio” |
| :checkbox | $(‘:checkbox’); | Returns only input elements of the type “checkbox” |
| :submit | $(‘:submit’); | Returns only input elements of the type “submit” |
| :image | $(‘:image’); | Returns only input elements of the type “image” |
| :reset | $(‘:reset’); | Returns only input elements of the type “reset” |
| :file | $(‘:file’); | Returns only input elements of the type “file” |
| :button | $(‘:button’); | Returns only input elements of the type “button” |
| :enabled | $(‘:enabled’); | Returns all enabled input elements |
| :selected | $(‘:selected’); | Returns the selected element in a select list. |
| :disabled | $(‘:disabled’); | Returns disabled input elements |
| :checked | $(‘:checked’); | Returns checked input elements of the type radio or checkbox. |
Hopefully this reference table will help you on your way with jQuery selectors and attribute selector. Hope this helps.
For information on how to use the returned jQuery object from any of the selectors above please read this aticle: jQuery Object to jQuery DOM Element
Tags: DOM, JavaScript, jQuery, Reference
Categories: Blogs, JavaScript, Technical, jQuery
Maja Williams has written 16 articles.
Other articles by this author Maja Williams



hi :
thanks for this nice article, but its look like you have an error in the “:gt(n)” example , because you type it “$(’ul li:eq(n)’);” not ” $(’ul li:gt(n)’);”
regards
Hello,
Thanks very much for this table, very useful !
I have a few comments regarding your table :
1. “$” operator : I thought it was better to use the “jQuery” operator to avoid any conflict with other libraries (Prototype,…) ? (ie jQuery(“#id”))
2. Example for the selector “:gt(n)” : the example should be “$(‘ul li:gt(n)’);” and not “$(‘ul li:eq(n)’).
3. HTML code for the table : it would be great if you could added the “scope=’col’” attribute on the table “th” in order to make the table accessible.
/Brett
Thanks guys. All suggestions have been gratefully excepted and the article amended. Remember guys I spent all this time writing this article to help you so comments are very welcome.
Thanks
Glyn
Nice job
Many, many thanks for this note. I have been pulling my hair out for about an hour trying to figure out why I couldn’t get $.post to recognize my JSON result. I hope they update their documentation soon.
To be honest the documentation on jQuery is fine, just a little hard to get around
. All I did was spend a few hours consolidating it into one list rather than all different pages.
Thanks for the comment
[...] jQuery selector. jQuery selectors and attribute selectors are some of the best features jQuery has to offer when it comes to DOM manipulation. They allow you to quickly select all elements or groups of element of a given tag… [...]
Hi,
You haven’t mention here about jQuery AJAX methods
$.get(url,data,function(result){});
$.get(post,data,function(result){});
$.ajax{(url:’url’,data:’data’,contenttype:’contenttype’,success:function(){})});
and also please tell us about
$(‘#selector’).bind(function(){});
$(‘#selector’).live(“onpaste”,function(){});
these kind of events
Kind Regards,
Saurabh
Well yes, thanks for that. Nice list. Appreciated
Fantastic list! Just an FYI regarding an error though:
This is a great site it has really helped me understand the selector. I would like to add something that I wanted to know when I started looking at this list. I wanted to know right away how to do something like an AND Statement. I found after hitting the jQuery message boards that you can do something like $(‘[attribute*=value][attribute*=value]‘) and this will act like an AND statement. It may be clear to others that this is a valid but It wasn’t to me so It may be worth putting an explanation in this document.
[...] http://www.pamaya.com Powered by Max Banner Ads Share and Enjoy: [...]
Hi Maja,
This selectors list really helped me.
Thank you very much for posting this!
I also have twitted this:
http://twitter.com/manakor/status/9439232564
This is really nice site and it really helped me to understand Jquery selectors.
Thanks,
Shree
Nice i knew most and not sure how i got here now heh. But i tend to use the following
jQuery(function($) {
});
This will stop any possibly conflicts that might arise and you can still use $ inside.
Hello Everyone,
JQuery selector is used to select HTML element by element name or attribute. JQuery selectors and attribute selectors are some of the best features. They allow you to quickly select all elements or groups of element of a given tag name, attribute name or their contents. In this example I am trying to change backcolor and forecolor of text that defined in element on mouse over…………………… for more details chekc out this link…
http://mindstick.com/Articles/7cb3c8f0-2329-457e-9714-c65ba49aa826/?JQuery%20Element%20Selector#up
Thanks !!!!!
[...] this helps. For further information about selectors please read my article jQuery selectors and attribute selectors reference and examples. Posted in Articles, Technical Copyright 2011@websites-r-us.co.uk Privacy [...]
In your entry about
a+b $(‘li + a’);, I believe you meant to say sibling, not descendant.Here is a link to an example (not mine) that I tested and verified.
Until next time,
My your code be good code.
-Anton