02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

.attr()<br />

Sets one or more attributes for the set of matched elements.<br />

.attr(attribute, value)<br />

.attr(map)<br />

.attr(attribute, function)<br />

Parameters (first version)<br />

• attribute: The name of the attribute to set<br />

•<br />

value: A value to set for the attribute<br />

Parameters (second version)<br />

• map: A map of attribute-value pairs to set<br />

Parameters (third version)<br />

• attribute: The name of the attribute to set<br />

•<br />

function: A function returning the value to set<br />

Return Value<br />

The jQuery object, for chaining purposes.<br />

[ 63 ]<br />

Chapter 4<br />

Description<br />

The .attr method is a convenient and powerful way to set the value of attributes<br />

especially when setting multiple attributes or values returned by a function. Let's<br />

consider the following image:<br />

<br />

.attr(attribute, value)<br />

We change the alt attribute by putting 'alt' followed by a comma and the new<br />

value inside the .attr method's parentheses:<br />

$('#greatphoto').attr('alt', 'Beijing Brush Seller');<br />

We can add an attribute in the same way:<br />

$('#greatphoto').attr('title', 'Beijing Brush Seller – photo<br />

by Kelly Clark');

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

Saved successfully!

Ooh no, something went wrong!