Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of using attr() over addClass in jquery

What are the benefits of using attr() instead of using addClass() in jquery?

like image 443
Steffan Harris Avatar asked Feb 01 '12 04:02

Steffan Harris


People also ask

What is the purpose of attr () function in jQuery?

jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.

What is the use of ATTR?

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.

What is the use of addClass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


1 Answers

The only benefit of attr() I know if is that it works with SVG, addClass() does not.

This answer has some details https://stackoverflow.com/a/10257755/2393562

like image 126
Sauce Avatar answered Oct 19 '22 06:10

Sauce