简体   繁体   中英

How to print an id get with document.getElementById?

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <script type="text/javascript" src="fuctions.js"></script>
    <title>Count JS</title>
</head>
<body>
    <fieldset>
        <input type="number" value="1" id="num" min="1" max="5"/>
        <button  onclick="getValue()">
            Submit
        </button>
    </fieldset>
</body>
</html>

JS:

function getValue() {
var x = parseInt(document.getElementById("num"));
alert(x);

}

I just want to print this value that I get using document.getElementById , but when I print appears it:

在此处输入图片说明

Can someone help?

Add .value after document.getElementById

function getValue() {
    var x = parseInt(document.getElementById("num").value);
    alert(x);
}

.value returns value from input, in your case, you try convert DOMNode to Integer that will return NaN

var x = parseInt(document.getElementById("num").value);

Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM