Home  >  Article  >  Web Front-end  >  How to change id in javascript

How to change id in javascript

藏色散人
藏色散人Original
2021-07-02 14:39:565287browse

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.

How to change id in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn