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

Can javascript get the value of input?

青灯夜游
Release: 2023-01-05 16:10:04
Original
5285 people have browsed it

Javascript can get the value of the input. Method: first use the "document.getElementById("id value")" statement to obtain the input element object; then use the "input element object.value" statement to obtain the input element Just value.

Can javascript get the value of input?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript can get the input value

Code example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>JavaScript中获取input元素value值</title>
    <link rel="stylesheet" href="">
    <script>
        function printInputValue() {
            var inputValue = document.getElementById("demo1").value;
            console.log(inputValue);
            document.getElementById("demo2").innerHTML = inputValue;
        }
    </script>
</head>
<body>
    <input type="text" id="demo1">
    <button onclick="printInputValue()">获取input元素value值</button>
    <p id="demo2"></p>
</body>
</html>
Copy after login

Run result:

Can javascript get the value of input?

Description:

getElementById() method returns a reference to the first object with the specified ID.

The value attribute can set or return the value of the text field.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Can javascript get the value of input?. 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