jQuery attr() Method
jQuery attr() Method
The attr() method is used to set or return attributes and values of the selected elements. When attr() method is used to return the attribute value, it will return the value of the FIRST matched element. When attr() method is used to set attribute values, it will set one or more attribute/value pairs for the set of matched elements.
Syntax:-
- Its syntax for getting attribute value is:- $(selector).attr(attribute)
- Its syntax for setting attribute value is:- $(selector).attr(attribute,value)
- For setting attribute value with a function, its syntax is:- $(selector).attr(attribute,function(index,currentvalue))
- For setting multiple attributes and values, its syntax is:- $(selector).attr({attribute:value, attribute:value,...})
Further Explanation:-
Parameter | Description |
---|---|
attribute | Specifies the name of the attribute |
value | Specifies the value of the attribute |
function(index,currentvalue) | Specifies a function that returns the attribute value to set
|