JQuery attr()
The attr()
method in jQuery is used to get or set the value of an attribute for a selected element.
Syntax
The syntax to get the value of an attribute using attr()
method is:
$(selector).attr(attributeName);
The syntax to set the value of an attribute using attr()
method is:
$(selector).attr(attributeName, value);
Use
The attr()
method is useful when you need to get or set the value of an attribute for an HTML or XML element. It can be used for a variety of purposes, such as adding or removing a class
attribute, changing the src
of an img
element, or setting the title
of a link
element.
Example
Here is an example of using attr()
method to get and set the value of an attribute: