• 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 / JavaScript / Using Javascript to Remove the Id from a Div

Using Javascript to Remove the Id from a Div

March 13, 2022 Leave a Comment

We can use JavaScript to remove the id of a div by using the removeAttribute() method.

document.getElementById("div1").removeAttribute("id");

Let’s say I have the following HTML:

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

If we want to remove the id of div “div1” in the HTML above, we can target that div and then use the removeAttribute() method.

document.getElementById("div1").removeAttribute("id");

The resulting HTML would be as follows:

<div>
  <p>This is a paragraph.</p>
</div>

Using JavaScript to Remove the Id of an Div With a Click

We can remove the id of an HTML div using JavaScript very easily by combining the removeAttribute() method with a click event.

In this example, we will have a paragraph with a bold and underlined style. We will then remove the id of the paragraph so that it will have no styles. The HTML setup is pretty simple.

Here is the HTML code:

<style>#p1 { font-weight: bold; text-decoration:underline; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can remove the id of.</p>
  <div class="click-me" onclick="removeId1()">Remove id p1</div>
  <div class="click-me" onclick="setId1()">Set id p1</div>
</div>

We can utilize both the onclick event and the removeAttribute() method to remove the id of the paragraph #p1. If the paragraph has id “p1”, then it will have an underline and bold style. When we remove this id, it will have none of these styles.

Below are the two functions that will allow the user to be able to set or remove the id of the paragraph. We can set the id of an element too back to p1.

Here is the JavaScript code below:

function removeId1(){
  document.getElementById("p1").removeAttribute("id");
}; 
function setId1(){
  document.querySelector("#div1 p").id = "p1";
}; 

The final code and output for this example of using JavaScript to remove the id of a div with a click is below:

Code Output:

This is the paragraph that we can remove the id of.

Remove id p1
Set id p1

Full Code:

<style>#p1 { font-weight: bold; text-decoration:underline; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can remove the id of.</p>
  <div class="click-me" onclick="removeId1()">Remove id p1</div>
  <div class="click-me" onclick="setId1()">Set id p1</div>
</div>

<script>

function removeId1(){
  document.getElementById("p1").removeAttribute("id");
}; 
function setId1(){
  document.querySelector("#div1 p").id = "p1";
}; 

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to remove the id from a div.

Other Articles You'll Also Like:

  • 1.  JavaScript String endsWith Method
  • 2.  Using JavaScript to Capitalize the First Letter of a String
  • 3.  Remove Vowels From String In JavaScript
  • 4.  Using JavaScript to Replace Multiple Characters in a String
  • 5.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 6.  Remove the First and Last Character From a String in JavaScript
  • 7.  Remove Commas From a String in JavaScript
  • 8.  JavaScript slice – How to Get a Substring From a String
  • 9.  Using JavaScript to Show a Div
  • 10.  How to Use JavaScript to Round Up Numbers

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