Categories
JavaScript Answers

How to change the ID of an HTML element with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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