To remove an attribute from a HTML element using jQuery, the simplest way is to use the removeAttr() method.
$("#div").removeAttr("style");
Let’s say I have the following HTML:
This is a paragraph.
If we want to remove the attribute “style” from #div, we can use the jQuery removeAttr() method to do this with the following Javascript code.
$("#div").removeAttr("style");
The resulting HTML would be as follows:
This is a paragraph.
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#div").removeAttr("remove-class");
Removing a Attribute from HTML Element Using jQuery With a Click
Many times when creating a web page and the user experience, we want to remove certain styling and formatting based on an interaction with another element on the web page.
We can remove a class from a HTML element using jQuery very easily by combining the removeClass() method with a click event.
Let’s say we have the following HTML code and we want to give the user the ability to remove the style attribute from our div.
Click Me to Remove the style attribute from #div1
This is the paragraph.
We can utilize both the jQuery click() method and jQuery removeAttr() method to remove the style attribute from the div.
Below is the Javascript code which will allow the user to be able to remove the class from the paragraph:
$("#click-me").click(function(){
$("#div1").removeAttr("style");
});
The final code and output for this example of how to remove an attribute with a click using jQuery and Javascript is below:
Code Output:
Click Me to Remove the style attribute from #div1
This is the paragraph.
Full Code:
Click Me to Remove the style attribute from #div1
This is the paragraph.
<script>
$("#click-me").click(function(){
$("#div1").removeAttr("style");
});
</script>
Using jQuery to Remove Multiple Attributes from a HTML Element
We can use the jQuery removeAttr() method to remove multiple attributes to an HTML element very easily.
The key to removing multiple attributes to our HTML elements is putting a space in between the attributes we want to remove in the call to removeAttr()
For example, if we want to remove classes “class” and “style” from a specific div, we can do so with the following Javascript code:
$("#div").removeAttr("class style");
Let’s say we have the following HTML:
Click Me to Remove Attributes from the Div
If we want to remove the attributes”class” and “style” from this div after a click, we just need to do the following in our Javascript code:
$("#click-me").click(function(){
$("#div").removeAttr("class style");
});
The result will be that the div will not have the text aligned center and will not have all of it’s text underlined.
The final code and output for this example of how to remove multiple attributes using jQuery and Javascript is below:
Code Output:
Click Me to Remove Attributes from the Div
Full Code:
Click Me to Remove Attributes from the Div
<script>
$("#click-me").click(function(){
$("#div1").removeAttr("class style"); // this will remove the attributes from the div
});
</script>
Remove Data Attributes Using jQuery
In HTML5, we can use data attributes which start with “data-” to store data in the various HTML elements we have in our web pages.
To remove a data attribute, we can use removeAttr() in the same way as above to remove these data attributes:
$("#click-me").click(function(){
$("#div").removeAttr("data-attribute-1");
});
Hopefully this article has been useful for you to understand how to remove an attribute from a HTML element using jQuery.