jQuery Lesson Series: Introduction to Selectors
With this post I want to start a new cycle of lessons dedicated to jQuery after a lot of requests I received in the past weeks from many readers about this topic. In particular this article is an introduction to jQuery Selectors and illustrates how to start using them with simple practical examples. In the next articles I’m going to add more detailed information and tutorials in order to cover every aspect about Selectors and their usage. For a printable reference guide to the jQuery API I suggest you to download my jQuery Visual Cheat Sheet or take a look at the official jQuery documentation.
Introduction to Selectors
jQuery selectors allow you to get quickly one or more elements in the Document Object Model. Selectors are classified in Basic, Hierarchy, Basic Filters, Content Filters, Visibility Filters, Attribute Filters, Child Filters, Form, Form Filters. In the following paragraph we’re going to see how to use some Basic Selectors, Attribute Filters, Basic Filters and Content Filters.
Basic Selectors
Basic Selectors are the most simple way to get a single element or group of elements with jQuery. You can get a single element with a specific #id and set the html content with “Hello World!” in this way:
$("#my-paragraph").html("Hello World!");The following code allows you to get all elements contained into the element with ID #section and sets, for each matched element, the CSS background property to the value #D1D1D1.
$("#section span").css("background","#D1D1D");You can do the same thing using a CSS class selector. Here is the code that gets all elements with the attribute class with the value mylist and sets the CSS background property to #D1D1D:
$(".mylist").css("background","#D1D1D");Here is a very simple example of code that gets on click event the element with ID #my-paragraph, sets the html content to “Hello World” and color property to #444:
$(document).ready(function(){
$("#my-paragraph").click(function (){
$(this).html("Hello World");
$(this).css("color","#444");
});
});Attribute Filters
Attribute filters allow you to get elements with a specific attributes. The syntax is simple: $("element[attribute]"). The following code get all
elements that have the attribute #ID specified and sets on click event the html content to “Paragraph with ID attribute!”.
$("p[id]").click(function (){
$(this).html("Paragraph with ID attribute!");
});
This code finds instead all element that have the attribute class not with the value “intro”.
$("p[class!=intro]").append("Paragraph with class");Another useful is access to: Finds all inputs with a href attribute that contains a part of text (in this example the URL of my blog ‘woorkup.com’) and sets the value of the attribute class to .mysite.
$("a[href*='woorkup.com']").addClass("mysite");Basic Filters
Basic filter are useful to access one or more elements with several criteria, for example the first or last element of a DOM element, odd or even elements, a single element by its index and so on.
:first and :last – :first and :last allow you to get to the first and last element of a specific DOM element. For example you can get easily the first an last element of a
list with a specific ID in this way. This is the html code for the list:
- Item A
- Item B
- Item C
- Item D
Here is a simple code that gets the first item of the list with id #t-myul-1 and sets the CSS color property with the value #0033CC. Then the code gets the last list item and sets the attribute class to last:
$("#t-myul-1 li:first").css("color", "#0033CC");
$("#t-myul-1 li:last").addClass("last");
And this is the CSS class .last:
.last{
background:#0033CC;
color:#FFF;
}Here is the result (See the demo):

Practical application. Simple Vertical Scroller – :first and :last are very useful to implement a simple vertical or horizontal scroller. The following example describes you how to develop a simple vertical scroller in one minute using an
list and just some lines of JavaScript code. This picture illustrates how the scroller works:

Here is the html code for the list:
- Item 1
- Item 2
- Item 3
- Item 4
Scroll up
This is the result in a web browser:

The element Scroll up allows you to scroll up the list on click event. Here is the JavaScript code for jQuery to scroll up the list:
$(document).ready(function() {
$("span[name='scroll']").click(function scroll_up(){
first = $("#t-myul-2 li:first");
last = $("#t-myul-2 li:last");
first.clone(true).insertAfter(last);
first.remove();
});
});
The previous code can be easily reused and customized with a little effort to implement advanced vertical or horizontal scrollers with more complex effects and animations. See the demo.
:even and :odd – :even and :odd get even and odd elements. A typical situation in which these filters are useful is when you want to highlight with different colors even and odd items of a list. Image to have the following list with #id t-myul-3:
- Item A
- Item B
- Item C
- Item D
Define two CSS classes .even and .odd to highlight even and odd elements in your list:
.even{background:#EFEFEF;}
.odd{background:#FFF;}
Here is the JavaScript code for jQuery to highlight with different colors even and odd list items:
$(document).ready(function() {
$("#t-myul-3 li:odd").addClass("odd");
$("#t-myul-3 li:even").addClass("even");
});
The first line of the previous code $("#t-myul-3 li:odd").addClass("odd") gets all odd list items within the element with id #t-myul-3 (in this example an unordered list) and add to each given element the CSS class .odd. And here is the result (See the demo):

Content Filters
Content Filters allow you to get elements by their content. This example illustrates how to highlight one or more list item containing a specific text (in this case “Woork Up”). This is the
list with ID #t-myul-4:
- Mashable
- Smashing Magazine
- Woork Up
- Noupe
This is the CSS code I used to define the class .highlight:
.highlight {
background:#FFC;
}And here is the Javascript code for jQuery:
$(document).ready(function() {
$("#t-myul-4 li:contains('Woork Up')").addClass("highlight");
});Here is the result:

You can also use :contain combined with :even and :odd in this way:
$(document).ready(function() {
$("#t-myul-4 li:even").addClass("even");
$("#t-myul-4 li:odd").addClass("odd");
$("#t-myul-4 li:contains('Woork Up')").addClass("highlight");
});And here is the final result (See the demo):

Conclusions
In this post we’ve started learning how to use some jQuery Selectors that allow you to move first steps with jQuery. In the next articles I’m going to cover every aspect about Selectors and their usage. For a list of all available Selectors take a look at the official jQuery Documentation. If you are a developer I suggest you to download my jQuery 1.3 Visual Cheat Sheet. Comments and suggestions are appreciated.
Hey Antonio,
It’s nice post but i think, you should start to use syntaxhighlighter plugin for your code samples. For sure it will be better than now. ;)
Anyway, thanks for the post!
With the new blog I prefer don’t use colors for highlight code. I think it’s better…
Ok mate, if you said so ;)
never mind the highlighting.. imho a nice way to make code more readable is to make as much spacing between entities as possible. For example:
$( document ).ready( function(){
$( "#li.menu" ).hover( function()
{
$( this ).addClass( "hover" );
},
function()
{
$( this ).removeClass( "hover" );
}
} );
Just a tip ;)
Great Jquery Series! at Woork about Mootools lesson I can use mootools from your blog. Thankx
Ok, let’s start learning jQuery :D
I wait for the cont’d …
useful, thx
I want to learn Jquery. Thanks Antonio.
the first big post, after switching from blogger, nice to see, need more valuable PM and Web development stuff!
Hey, great tutorial.
I’ve never used jQuery, so I’ll definitely look into it now. And I agree with Yigit, a syntax highlighter would be great. They make the text more comprehensive, easier to read and understand.
I can’t wait to see more!
I actually read the post, with the PDF open by the side, was so worth it.
Thank you :)
A very great job. Hope to read your such kinds of articles again.
JQuery is actually easier to learn than I thought. Thank you pro blogger.
Thanks a lot of… It´s very cool. I have a question for anothe post of selectors. How I work with “this”?
For example in the last ul:
If I get in a variable the UL object, How Isis this:
$("#t-myul-4 li:even").addClass("even");Something like:
$(this & " li:even").addClass("even")?????Thanks!
Hello. Thanks for this resource.
Is there any way to get a list item’s position apart from :first and :last?
In other words, is there a way to return 1 for A, 2 for B, 3 for C … in this:
Item A
Item B
Item C
Item D
Thank you.
Jeremy
Nice tutorial for jquery newbies. Waitin’ for the other lessons.
Thanks for this. I have tried to learn jQuery soooooooo many times and not been successful yet. This was a great, SIMPLE way for me to start to understand it. I may now be able to start to bend it to my will because of your simple example. Most tutorials just go straight to how to accomplish xxxx, the most complicated chunks of code with a few lines of explanation. I find a good teaching article must give an example show what it does, then change it a little, and show how that affects it. That’s how folks learn.
This article along with Chris Coyier’s latest screen casts (where I finally made the connections necessary to understand) I’m on my way.
Thanks, Keep up the good work.
+1 subscriber
Nice tutorial. I fell in love with jQuery recently (about a year ago) and learn/find new stuff every day. I knew selectors like these existed, but this is a nice little tutorial you’ve written up. Kudos!
very nice tut! congrats
Good Tutorial , thanks Antonio :)
Thanks for this awesome tip, I will definitely be implementing something of this sort on my next project.
Lovely work :)
Nice write-up, seems like the demo links are currently broken ?
Thanks man. :)
Nice write-up