Home > Article > Web Front-end > How to change id in javascript
How to change the id in JavaScript: 1. Change the id value of the div through the byTagName method; 2. Change the id value of the div through the byId method.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to change the id in javascript?
Use JS to change the div id
By using JS, there are two ways to change the id value of the div.
1) byTagName
document.getElementsByTagName("div").id = 'id_new';
2) byId
document.getElementById("div_id").id = 'id_new';
However, in practical applications, byId is the safest way of writing and can be used at any time. ByTagName can only be used when the web page has only one div.
Recommended study: "javascript Advanced Tutorial"
The above is the detailed content of How to change id in javascript. For more information, please follow other related articles on the PHP Chinese website!