Sometimes, we want to change the ID of an HTML element with JavaScript
In this article, we’ll look at how to change the ID of an HTML element with JavaScript.
How to change the ID of an HTML element with JavaScript?
To change the ID of an HTML element with JavaScript, we set the id
property.
For instance, we write
<p id="one">One</p>
to add a p element.
Then we write
document.getElementById("one").id = "two";
to select the element with getElementById
.
And then we set its id
property to the new ID.
Conclusion
To change the ID of an HTML element with JavaScript, we set the id
property.