This question already has an answer here:
newbie here and I've created this javascript program where when the user enters a number greater than 21 a alert pops up saying they're legal, of if the number is less than, another alert pops up saying they are too young. I'm not sure if I'm using the onclick event the right way. But when I click the button nothing happens, but when i reload the page then the alerts pop up. Not sure whats wrong, any suggestions? Thanks in Advance.
Here is my code
var button = document.getElementById("button").onclick = legal();
function legal(){
var input = document.getElementById("input").value;
var age = 21;
if(input < age){
alert("Sorry, your not old enough");
}
else{
alert("Great, come on in! your legal");
}
}
<h1>
Are you Legal?
</h1>
<input type="number" id="input">
<button id="button" onclick="legal();">submit</button>
Aucun commentaire:
Enregistrer un commentaire