• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Remove Attribute from HTML Element

Using jQuery to Remove Attribute from HTML Element

November 30, 2021 Leave a Comment

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:

<div id="div" style="text-align:center;">
  <p class="p">This is a paragraph.</p>
</div>

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:

<div id="div">
  <p class="p">This is a paragraph.</p>
</div>

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.


<div id="div1" style="text-align:center;">
  <p id="click-me">Click Me to Remove the style attribute from #div1</p>
  <p class="p">This is the paragraph.</p>
</div>

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:


<div class="html-code-output">
<div id="div1" style="text-align:center;">
  <p id="click-me">Click Me to Remove the style attribute from #div1</p>
  <p class="p">This is the paragraph.</p>
</div>
</div>

<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:

<style>.class-1 { text-decoration:underline; }</style>
<div id="div" style="text-align:center;" class="class-1">
  <p id="click-me">Click Me to Remove Attributes from the Div</p>
</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:

<style>.class-1 { text-decoration:underline; }</style>
<div class="html-code-output">
<div id="div1" class="class-1" style="text-align:center;">
  <p id="click-me">Click Me to Remove Attributes  from the Div</p>
</div>
</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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Convert a String to Uppercase
  • 2.  How to Check if Element Exists Using jQuery
  • 3.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 4.  Using jQuery to Change Div Text
  • 5.  jQuery Get Last Child
  • 6.  Get Padding of an Element Using jQuery
  • 7.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 8.  Check if Input is Empty Using jQuery
  • 9.  Using jQuery to Add Class to HTML Element
  • 10.  Using jQuery to Add Option to Select List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy