I am having trouble getting my javascript code to take an input from my HTML input tag.
The basic function of the program is for the user to enter a number, and the program tells the user if their number is positive, negative, or zero.
Here is my code:
<!DOCTYPE html> <html lang="en"> <head> <script type="text/javascript"> window.onload = function () { if (Math.sign("number") = 1) { document.getElementById("demo").innerHTML = "Your number is positive"; } else if (Math.sign("number") = -1) { document.getElementById("demo").innerHTML = "Your number is negative"; } else { document.getElementById("demo").innerHTML = "Your number is zero"; } } </script> </head> <body> <form> <input type="number" type="button" id="number"> <input type="submit" value="submit"> </form> <p id="demo"></p> </body> </html>
Advertisement
Answer
function myFunction(){ let number = document.getElementById("number").value if (number > 0) { document.getElementById("demo").innerHTML = "Your number is positive"; } else if (number < 0) { document.getElementById("demo").innerHTML = "Your number is negative"; } else { document.getElementById("demo").innerHTML = "Your number is zero"; } }
<input type="number" type="button" id="number" /> <button onclick="myFunction()">Submit </button> <p id="demo"></p>