jQuery Attributes
Attributes
We can easily manipulate all the attributes given to an element with the help of jQuery. These attributes can be src, href, id, class, style, width, height, and many more.
The only method used for this purpose is attr(). This method can be used to either get the attribute value or it can be used to set the attribute value. IN other words, it can perform both actions i.e, get attribute value and set the attribute value.
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
|