DevRyu
DevRyu

Reputation: 75

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:

enter image description here

Can someone help?

Upvotes: 0

Views: 3012

Answers (2)

Oleksandr T.
Oleksandr T.

Reputation: 77482

.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

Upvotes: 2

glyuck
glyuck

Reputation: 3397

Add .value after document.getElementById

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

Upvotes: 1

Related Questions