Am new to js and would like to use values from an input in my js 'if' code. I wrote the code below but it does not render. Can you tell me what i did wrong.
function myFunction() { var age = document.getElementById('selection').value; if (age < 4) { alert('The child is too young'); } else if (age > 40) { alert('You are too old for this course'); } }
<form onsubmit=" myFunction()"> Age: <input type="text" id="selection" name="age"> <input type="submit" value="submit"> </form>
you are very close.
this one should work
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>js loops</title>
<link rel="stylesheet" href="loops.css">
<script>
function myFunction() {
var age = document.getElementById('selection').value;
if (age < 4) {
alert('The child is too young');
} else if (age > 40) {
alert('You are too old for this course');
}
return false;
}
</script>
</head>
<body>
<form onsubmit="return myFunction()">
Age: <input type="text" id="selection" name="age">
<input type="submit" value="submit">
</form>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.