02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

54<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

>>> $("p:eq(3)").attr("id");<br />

"bar"<br />

Next, change the ID attribute of the last paragraph to "bat" using this code:<br />

$("#bar").attr("id", "bat");<br />

After execution, the following displays in the console:<br />

>>> $("#bar").attr("id", "bat");<br />

[ p#bat ]<br />

Now, if you try to select elements with an ID of bar, an empty result set is returned:<br />

>>> $("#bar");<br />

[ ]<br />

However, you can now select a paragraph element with an ID of bat:<br />

>>> $("#bat");<br />

[ p#bat ]<br />

Additionally, multiple attributes can be set using JSON format:<br />

$("p:eq(3)").attr({<br />

"id":"baz",<br />

"title":"A captivating paragraph, isn't it?"<br />

});<br />

After executing this code, the HTML panel of Firebug reveals that the paragraph’s markup has been<br />

changed:<br />

<br />

.removeAttr()<br />

To remove an attribute, simply call .removeAttr() on the element from which you wish to remove the<br />

attribute <strong>and</strong> pass the attribute’s name.<br />

Enable the check box in the sample form <strong>by</strong> removing the disabled attribute:<br />

$(":checkbox").removeAttr("disabled");<br />

After executing this code, the check box can now be checked <strong>and</strong> unchecked at will.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!