Thursday, September 29, 2011

attr(String,Function)

attr(String key, Function value) returns jQuery
Set a single property to a computed value, on all matched elements. Instead of supplying a string value as described

[[DOM/Attributes#attr.28_key.2C_value_.29|above]], a function is provided that computes the value.

Example:

Sets title attribute from src attribute.

$("img").attr("title", function() { return this.src });
HTML:
<img src="test.jpg" />
Result:
<img src="test.jpg" title="test.jpg" />

No comments:

Post a Comment