Home>Article>Web Front-end> How to use getElementById in JavaScript

How to use getElementById in JavaScript

不言
不言 Original
2018-12-24 10:30:12 24926browse

getElementById is a method that returns an element with a specified id value as an Element object. You can use getElementById to obtain the specified ID from the HTML tag to process certain content. Let's take a closer look at how to use getElementById.

How to use getElementById in How to use getElementById in How to use getElementById in JavaScript

Usage of getElementById

The way to get the element using getElementById is as follows

document.getElementById(id)

Set the id you want The id element obtained in HTML. Since the same id cannot be used multiple times in HTML, it must be a unique id value.

If there are multiple ids in the HTML, only the first matching id element is returned. If the specified id does not exist, null is returned.

Let’s look atspecific examples

Display characters

The code is as follows

       

The running results are as follows

How to use getElementById in How to use getElementById in How to use getElementById in JavaScript

Display the connection text

       

The running results are as follows

How to use getElementById in How to use getElementById in How to use getElementById in JavaScript

Display calculation results

       

The running results are as follows

How to use getElementById in JavaScript

##Finally, Since there are many uses of getElementById in DOM (Document Object Model), we need to master it as much as possible.

The above is the detailed content of How to use getElementById 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