jQuery Attributes

To manipulate an element property using jQuery, we can use it's attributes assigned to them. Like, an element may have classname, id, title, src, href. jQuery provide methods to manipulate elements based on their attributes.

jQuery Attribute Methods

Methods Description
addClass() Add class to each elements in the matched set.
attr() Set the value to all matched elements.
hasClass() Check the existence of specified class in the set of matched elements.
html() Get the HTML contents of the first matched element.
html(val) Set the HTML contents of every matched elements.
removeAttr() Remove specified attributes from each of the matched elements.
removeClass() Remove all specified classes from the set of the matched elements.
toggleClass() It adds or removes the specified class if it is not present or present respectively.
val() Get the input value from the first matched element.


.cls {
color: red; 

<p id="demo"> Hello World!</p>

   $("p#demo").attr('class', 'cls');

Output of the above code

Hello World!

Further, we have explained the most common attributes in brief.