12.3. Selecting Elements

jQuery's strength is the ease with which you can select and manipulate elements on the page. You have used a selector method already to select all <div> elements that have an ID of div1:

$("#div1").html("hello jQuery");

jQuery supports many different ways to select an element or elements that should meet all but the most awkward requirements. Table 12-1 lists examples of other selection methods you might want to use.

Table 12.1. Example Selection Methods
SelectorSelected
$("#div1")Selects a <div> with an ID of div1.
$("div")Selects all <div>s.
$("div.standardDiv")Selects all <div>s with a class of standardDiv.
$(".standardDiv")Selects all elements with a class of standardDiv.
$("#div4 #div5")Selects a <div> with an ID of div5 nested inside a <div> with an ID of div4.
$(".standardDiv")[0].innerHTML="hello jQuery";Selects the first element of a group of <div> with a class of standardDiv and sets the innerHTML property to "hello jQuery". Note that the use of traditional properties and jQuery selectors is sometimes possible.

12.3.1. CSS Selectors

In addition to the previous standard selectors, more modern selectors are also available (browser support may differ, but you are probably safe with IE 7+ and Firefox 3+); see Table 12-2.

Table 12.2. CSS Selectors
SelectorSelected
$("div:first")Selects first <div>
$("div:last")Selects last <div>
$("div:even")Selects even-numbered <div>s
$("div:odd")Selects odd-numbered <div>s
$("div:first-child")Selects first child element
$("div:last-child")Selects last child element
$("div:nth-child(3)")Selects third child element
$("a[href^=http://]")Selects any hyperlink starting with the text http://
$("a[href$=.zip /]")Selects any hyperlink ending with .zip
$("a[href*=Microsoft")Selects any hyperlink with the text Microsoft in it
$("input[type=button]")[0].innerText="hello jquery2"Selects first input element of type button and changes innerText property to "hello jquery2"
$(":checked")Gets all check boxes that are selected
$(":disabled")Selects all disabled elements
$(":enabled")Selects all enabled elements

12.3.2. jQuery Selectors

jQuery also provides some built-in selectors; see Table 12-3.

Table 12.3. jQuery Selectors
SelectorEffect
$(":button")[0].innerText="hello jquery2";Changes the innerText property of first button element
$(":contains(alex)")Selects all elements containing the text alex
$(":hidden")Selects all hidden elements
$(":selected")Selects all selected elements
$(":visible")Selects all visible elements
$("div:not(.standardDiv)")Selects all <div> elements that do not have a class of standardDiv

12.3.3. Working with Sets

So, you have used one of the preceding selector methods and now have a collection of matching elements (a wrapped set). How can you work with them? jQuery has a number of different methods to work with collections; see Table 12-4.

Table 12.4. Different Ways to Manipulate Sets
ExampleMeaning
$("div").get(0)Gets first element in set
$("div")[0]Gets first element in set
$("div").get()Gets all elements in set
$("div").size()Gets size of set

12.3.4. .each() Method

The .each() method works like the foreach statement in C# or Visual Basic and allows you to iterate through a set of elements. The following example iterates through a set of <div>s, modifying the innerHTML property on each element:

function showDivNumber()
{
    $("div").each(function(index) {
        this.innerHTML = "I am div number " + index;
    });
}

Of course, if you just wanted to set the same text to each <div> element, you could apply the change to the wrapped set as follows:

$("div").html("I am a div");

12.3.5. Working with Attribute Values and CSS

jQuery provides methods to read, set, and remove attributes on page elements; see Table 12-5.

Table 12.5. Examples of Working with Attributes
ExampleMeaning
alert($("#div1").attr("id"));Retrieves the id attribute from <div>
$("#div1").attr("title", "hello");Sets the title attribute to "hello"
$("#div1").removeAttr("title")Removes the title attribute
$("#div1").addClass("specialDiv");Adds the specialDiv CSS class to element

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.210