Home

jQuery Selector Examples

Basic Syntax

The basic syntax of a jQuery command could be...

$(selector).action();

eg (hiding an object)...
$(this).hide(); (this relates to the current object)
$("p").hide(); (all the <p> elements)
$(.test).hide(); (this relates to the element with the class="test")
$(#test).hide(); (this relates to the element with the id="test")

so $() is basically the same as saying document.getElementsByTag() or one of the other getElement options

You can also chain commands together on one line...eg...
$("#test").css("color", "red")
.slideUp(2000)
.slideDown(2000)
.html("New Text");


NOTE...
So far it seems that jQuery is just a bunch of METHODS or FUNCTIONS
which means that changes are all in brackets...eg...
.html("New Text") and not .html="New Text"

Some examples of Syntax

Note...if you head to w3schools.com you will see some examples where you can test out the below commands.

This uses a <table> to create this
Syntax Meaning
$("*") Selects all elements
$("this") Selects the current HTML element
$("p.intro") Selects all <p> elements with class="intro"
$("p, .intro") Selects all <p> elements AND all class="intro" elements
$("p:first") Selects the first <p> element
$("ul li:first") Selects the first <li> element of the first <ul>
$("ul li:first-child") Selects the first <li> element of the every <ul>
$("[href]") Selects all elements with an href attribute
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"
$(":button") Selects all <button> elements and <input> elements of type="button"
$("tr:even") Selects all even <tr> elements
$("tr:odd") Selects all odd <tr> elements