Home >Web Front-end >JS Tutorial >Can javascript get the value of input?

Can javascript get the value of input?

青灯夜游
青灯夜游Original
2021-04-20 15:39:135319browse

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>

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!

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