In: Computer Science
Develop and test an HTML document to use the DOM 2 event model
that has text boxes for apple (59 cents each), orange (49 cents
each), and banana (39 cents each), along with a Submit button.
These text boxes take a number, which is the purchased number of
the particular fruit. Add reality checks to the text boxes of the
document to ensure that the input values are numbers in the range
from 0 to 99. Each of the text boxes should have its own onclick
event handler. These handlers must add the cost of their fruit to a
total cost. An event handler for the Submit button must produce an
alert window with the message Your total cost is $xxx.xx, where
xxx.xx is the total cost of the chosen fruit, including 7.75
percent sales tax. This handler must return false (to avoid actual
submission of the form data). You also must use an external style
sheet to display various input boxes in appropriate background
color.
After creating, testing, and validating the HTML5 and CSS
documents, publish them together with your Javascript code file on
your document root on the server.
HTML code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css href="main.css">
</head>
<body>
<script type="text/javascript">
function calculate()
{
var apple_count = document.program.apple.value;
var orange_count = document.program.orange.value;
var banana_count = document.program.banana.value;
sales_tax=7.75/100;
total_cost = ((apple_count*59+orange_count*49+banana_count*39)+sales_tax).toFixed(2);
alert("Your total cost is $"+total_cost);
}
</script>
<div class="container">
<form name="program">
<label for="number">Enter the number of apple (59 cents each):</label>
<input type="number" id="apple" name="apple" min="0" max="99"><br><br>
<label for="number">Enter the number of orange (49 cents each):</label>
<input type="number" id="orange" name="orange" min="0" max="99"><br><br>
<label for="number">Enter the number of banana (39 cents each):</label>
<input type="number" id="banana" name="banana" min="0" max="99"><br><br>
<input type="button" value="Submit" onclick="calculate();"><br><br>
</form>
</div>
<p id="demo"></p>
</body>
</html>
main.css
input[id=apple] {
box-shadow: 2px 2px 7px red;
}
input[id=orange] {
box-shadow: 2px 2px 7px orange;
}
input[id=banana] {
box-shadow: 2px 2px 7px yellow;
}
input[type=button] {
box-shadow: 3px 3px 20px white;
padding: 20px;
background-color: #0B005B;
color:white;
}
.container {
border-radius: 5px;
background-color: #020237;
padding: 20px;
color: white;
box-shadow: 2px 2px 7px black;
}
output: