Home > Web Front-end > JS Tutorial > body text

How to change id in javascript

藏色散人
Release: 2023-01-07 11:41:46
Original
5348 people have browsed it

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';
Copy after login

2) byId

document.getElementById("div_id").id = 'id_new';
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template