This page is a tutorial on jQuery's selectors. If you don't know the basics, see: jQuery Basics Tutorial.
Here's a most basic example of getting a element and do something with it.
// make all paragraphs red $("p").css("color","red");
// make all “p” tag elements red $("p").css("color","red");
Test yourself: jQuery Select By Tag Example
// make element of id “xyz” red $("#xyz").css("color","red");
Test yourself: jQuery Select By ID Example
// make element of class “xyz” red $("[class='xyz']").css("color","red");
Test yourself: jQuery Select By Class Value Example
jq can select any tag attribute matching their value in many ways.
| Meaning | Syntax |
|---|---|
| Attribute Equals | myAttribName="myValue" |
| Attribute Not Equal | myAttribName!="myValue" |
| Attribute Starts With | myAttribName^="myValue" |
| Attribute Ends With | myAttribName$="myValue" |
| Attribute Contains | myAttribName*="myValue" |
| Attribute Contains Prefix | myAttribName|="myValue" |
| Attribute Contains Word | myAttribName~="myValue" |
Here's a example of getting all elements that has attribue “abc” with value “xyz”.
// select elements with attribute “abc” that has value “xyz”, make it red. $("[abc='xyz']").css("color","red");
The syntax follows CSS's syntax. If you are not familiar with CSS seletor syntax, see: CSS Tag Matching (Selector) Syntax.
To get elements without using jQuery, see: JavaScript: Get Elements by ID, Tag, Name, Class.