• 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 / How to Change the Id of an Element in JavaScript

How to Change the Id of an Element in JavaScript

March 13, 2022 Leave a Comment

To change the id of an element in JavaScript we can do this by combining the getElementById() method along with the id property.

document.getElementById("div1").id = "div2";

Let’s say I have the following HTML:

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

If we want to change the id of the div from “div1” to “div2” we could use the id property to do this with the following JavaScript code.

document.getElementById("div1").id = "div2";

The resulting HTML would be as follows:

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

How to Change the Id of an Element in JavaScript With a Click

We can change the id of an HTML element using JavaScript very easily by combining the id property with an onclick event.

In this example, we will have a paragraph with an id that has certain styles. We will replace that id with another id that has different styles. The HTML setup is pretty simple.

Here is the HTML code:

<style>#p1 { text-decoration:underline; } #p2 { font-weight: bold; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can change the id of.</p>
  <div class="click-me" onclick="changeId1()">Change to id p2</div>
  <div class="click-me" onclick="changeId2()">Change to id p1</div>
</div>

We can utilize both the id property and the getElementById() method to change the id of the paragraph to either #p1 or #p2. If the paragraph has id “p1”, then it will have an underline style. If it has id “p2”, then it will be bold.

We will use two onclick events that will run either function we create below. We will put the code above in our functions to allow the user to be able to change the id of the paragraph

Here is the JavaScript code:

function changeId1(){
  document.getElementById("p1").id = "p2";
}; 
function changeId2(){
  document.getElementById("p2").id = "p1";
}; 

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

Code Output:

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

Change to id p2
Change to id p1

Full Code:

<style>#p1 { text-decoration:underline; } #p2 { font-weight: bold; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can change the id of.</p>
  <div class="click-me" onclick="changeId1()">Change to id p2</div>
  <div class="click-me" onclick="changeId2()">Change to id p1</div>
</div>

<script>

function changeId1(){
  document.getElementById("p1").id = "p2";
}; 
function changeId2(){
  document.getElementById("p2").id = "p1";
}; 

</script>

Hopefully this article has been useful for you to understand how to change the id of an element in JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Get the Cursor Position in JavaScript
  • 2.  Reverse Words in a String JavaScript
  • 3.  Using JavaScript to Remove Quotes From a String
  • 4.  How to Find the Longest String in an Array in JavaScript
  • 5.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 6.  Remove String From Array in JavaScript
  • 7.  Using JavaScript to Round to 4 Decimal Places
  • 8.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 9.  Using JavaScript to Replace Multiple Characters in a String
  • 10.  Remove Brackets from String Using JavaScript

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