Tuesday, 15 May 2012

jQuery Selectors

jQuery offers a powerful set of tools for matching a set of elements in a document

Element Selector
Notation:$(“element”)                    
Ex : To select all input types use $(“input”)
<input type=“text”  id=“userame” name=“username” value=“rayudu”/>
<input type=“text”  id=“age” name=“age” value=“29”/>
<input type=“text” id=“dob” name=“dob” value=“18-09-1978”/>


ID Selector
Notation:$(“#id”)
Ex : To select element with id dob use $(“#dob”)
<input type=“text”  id=“userame” name=“username” value=“rayudu”/>
<input type=“text”  id=“age” name=“age” value=“29”/>
<input type=“text” id=“dob” name=“dob” value=“18-09-1978”/>

Class Selector
To select all elements using CSS class
Notation:$(“.classname”) 
Ex: $(“.customStyle”)
<input type=“text” class=“customStyle”  id=“userame” name=“username”/>
<div class=“newStyle”  id=“display1”></div>
<div class=“customStyle”  id=“display2”></div>
<span class=“customStyle”  id=“display3”></span>

Ancestor Descendant
Selects all elements that are descendants of a given ancestor.
Notation:$(“E F”) 
Ex : $(“div input”)
             <div>
                  <input type=“text” name=“username”/>
                 <span>
                    <input type=“text” name=“rollno”/>
                 </span>
             </div>
                <input type=“text” name=“rollno”/>

:first-child, :last:child, :only-child :nth-child(n)
$(“li:first-child”);
$(“tr:last-child”);
$(“img:only-child”);
$(“tr:nth-child(2n)”);

Attribute Selectors
Has Attribute: $(“img[alt]”);
Begins With: $(“input[name^=my]”);
Ends With: $(“a[href$=.pdf]”);
Contains: $(“a[href*=microsoft]”);
Equals: $(“:checkbox[checked=true]”)
:not(s), :empty, :eq(n), :contains(s)

No comments:

Post a Comment