0 votes
in jQuery by

How do you set attribute in html using jQuery?

1 Answer

0 votes
by (4.4k points)

Use attr() method for setting and getting attribute in html elements in jQuery.

attr():- The attr() method sets or returns attributes and values of the selected elements.
When attr() method is used to return the attribute value, it returns the value of the FIRST matched element.
Syntax:- $("selector").attr("attribute");

When attr() method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.
Syntax:- $("selector").attr("attribute","value"):

Set multiple attributes and values:
Syntax:- $(selector).attr({attribute:value, attribute:value,...});

Set attribute and value using a function:
Syntax:- $(selector).attr(attribute,function(index,currentvalue))

Share:- Whatsapp Facebook Facebook

Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.