Solution.
<html>
<head>
<title>JavaSscript</title>
</head>
<script type="text/javascript">
function submitNum()
{
var txtInput = parseFloat(document.getElementById('txtInput').value);
if (txtInput < 10)
{
alert("The number is less than 10");
}
else if(txtInput > 10)
{
alert("The number is greater than 10");
}
else
alert('The number is equal to 10');
}
</script>
<body>
<input type="text" placeholder="Type any number" id="txtInput" />
<input type="button" onclick="submitNum()" value="Submit" />
</body>
</html>
Where have you done errors ?
1. When you do click, it calls only function submitNum() without other definitions. So you should insert needed variables in the function.
2. document.getElementById returns only DOM node (not a value from input tag), so you should take value. How can you do this ? That’s easy – use property document.getElementById('txtInput').value. But it is not the end. It returns only string. To get a number use function parseFloat() or parseInt().
Comments
Thanks so much for your help!
Leave a comment