dj0965
dj0965

Reputation: 135

Javascript document.getElementById issue

I am a beginner to javascript, and am trying to learn how to use text boxes because I can't seem to find a page that is simple enough for me. This is what I have, but when I click the button, it just returns [object HTMLInputElement]. Could someone please tell me what i did wrong and help me fix it? Thanks.

<html>
<head>
<script language="javascript">
function displaynumber(){
var number1= document.getElementById("num1");
alert(number1).value
}
</script>
<title>
ok
</title>
</head>
<body>
<form>
Input the first number<input type="text" id="num1">
<input type="button" value="submit" onClick="displaynumber()">
</form>
</body>
</html>

Upvotes: 0

Views: 90

Answers (2)

Thomas Junk
Thomas Junk

Reputation: 5676

You should correct your typo:

alert(number1).value

to

alert(number1.value)

Upvotes: 3

Quentin
Quentin

Reputation: 943108

You are alerting number1 and then trying to read the value property of the return value of alert().

Move the property accessor inside the function call.

alert(
    number1.value
);

Upvotes: 2

Related Questions