Back

.attr( name, [value] )

Get or set attributes on the current set

If value is provided, this will set the attribute on each element and return the current set for chaining

If value is not passed, this will return the value of the attribute for the first element in the current set

There is a difference between attr vs prop

Params

name{String}The name of the attribute
value{Any} [optional]A value to be set. Most values will be converted to a String before setting. Functions will be evaluted with (previousValue, index) and the return value will be set.

Returns

Value or DollarJS (chainable)

Examples

$('img').attr('title')
$('img').attr('title', 'Click Me')
$('img').attr('title', function(previousValue, index){ return 'Click Me'; })