HTML and CSS Reference
In-Depth Information
quantity = prompt("Please type a quantity greater than 0");
if (quantity <= 0)
{
alert("Quantity is not greater than 0.");
} else {
alert("Thank you for entering a quantity greater than 0.");
} // end if
} // end function promptQuantity
// ]]> -->
</script>
</head>
<body>
<h1>Using JavaScript</h1>
<input type="button" value="Click to enter quantity"
onclick="promptQuantity();" />
</body>
</html>
Save the document as quantityif2.html and display it in the browser. Open the Error
Console in case there are typing errors when you run the script.
Click the button to test the script. If the prompt box does not appear, check the Error
Console and correct any errors. Figure 14.17 shows the browser and prompt box after
the button has been clicked, and the resulting alert box. Be sure to test for a value
larger than 0 and a value of 0 or less.
Figure 14.17
The browser on the
left shows the
prompt box and
input; the browser
on the right shows
the alert box
displayed after the
input
CHECKPOINT 14.3
1. Describe a method that can be used to gather a piece of data such as the user's age.
2. Write the JavaScript code to display an alert message for users who are under 18 years
old and a different alert message for users who are 18 years or older.
3. What is a function definition?
 
Search WWH ::




Custom Search