jQuery removeAttr() Attribute

The jQuery removeAttr() method is used to remove attributes from HTML elements. To use this method, simply pass it a string value of the attribute you'd like to remove. It removes attribute from each of the selected elements.

Syntax of removeAttr() Attribute

$(selector).removeAttr(attribute) 

The selector is the element from which attribute to be removed. The attribute is the required parameter. This is the name of the attribute to remove.

Example - remove single attribute

<style>
.cls { 
 color : red;
}
</style>

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

<script>
$(document).ready(function(){
$("p#demo").removeAttr("class");
});
</script>

Output of the above code

Hello World!

In the above example, the class attribute is removed from the paragraph of the id 'demo'.

Example - remove multiple attributes

<style>
.cls1 { 
 color : red;
}
#demo1 {
 font-size : 22px;
}
</style>

<span id="demo1" class="cls1"> Test Content</span>

<script>
$(document).ready(function(){
$("span").removeAttr("class id");
});
</script>

Output of the above code

Test Content

In the above example, class and id attributes are removed from the span.




Read more articles


General Knowledge



Learn Popular Language