In: Computer Science
Using Javascript
Create a page places an order for a Calzone:
Using Text box to get customers Name
Radio Buttons for sizes: small, medium, large
list for type of crust: crispy, soft, hard
check boxes for toppings, with at least 3 to be selected
Submit button that displays the order information.
Order.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- title for web page -->
<title>Calzone order</title>
</head>
<body>
<form>
<fieldset>
<legend>Place Order</legend>
<table>
<tr>
<td>Name</td>
<td>
<!-- <textbox> for name -->
<input type="text" id="txtName"/>
</td>
</tr>
<tr>
<td>Size</td>
<td>
<!-- radiobuttons fot size -->
<input type="radio" name="size" value="small"/>Small
<input type="radio" name="size" value="medium"/>Medium
<input type="radio" name="size" value="large"/>Large
</td>
</tr>
<tr>
<td>Type of Crust</td>
<td>
<!-- dropdown list -->
<select id="crust">
<option value="crispy">Crispy</option>
<option value="soft">Soft</option>
<option value="Hard">hard</option>
</select>
</td>
</tr>
<tr>
<td>Toppings</td>
<td>
<input type="checkbox" name="toppings" value="olive">Olive
<input type="checkbox" name="toppings" value="Bacon">Bacon
<input type="checkbox" name="toppings" value="Sausage">Sausage<br/>
<input type="checkbox" name="toppings" value="Onions">Onions
<input type="checkbox" name="toppings" value="Mushrooms">Mushrooms
<input type="checkbox" name="toppings" value="Pepperoni">Pepperoni
</td>
</tr>
<tr>
<td>
<!-- button to place order -->
<input type="button" value="Place Order" onclick="order()"/>
</td>
<td>
<!-- button to reset order -->
<input type="reset" value="Reset""/>
</td>
</tr>
</table>
</fieldset>
<!-- div to display order details -->
<div id="myOrder"></div>
</form>
<!-- <script> is used for javascript -->
<script>
//function order()
function order()
{
//getting name entered by user
var name=document.getElementById("txtName").value;
//getting radio buttons
var size=document.getElementsByName("size");
//getting type of crust
var crust=document.getElementById("crust");
//getting toppings
var toppings=document.getElementsByName("toppings");
//checking which size is selected
var selectedSize="";
//using for loop
for(var i=0;i<size.length;i++)
{
if(size[i].checked)
{
//get size
selectedSize=size[i].value;
}
}
//crust is selected
var selectedCrust=crust.options[crust.selectedIndex].value;
var countToppings=0;//variable to check selected toppings
var selectedToppings="";//variable to store selected toppings text
for(var i=0;i<toppings.length;i++)
{
//checking toppings
if(toppings[i].checked)
{
//concatenate selected toppings
selectedToppings+=toppings[i].value+" , ";
countToppings++;//count the toppings
}
}
//checking count of toppings
if(countToppings>=3)
{
var orderDetails="Name :"+name;
orderDetails+="<br/>Size : "+selectedSize;
orderDetails+="<br/>Crust : "+selectedCrust;
orderDetails+="<br/>Toppings : "+selectedToppings;
//display order details on the web page
document.getElementById("myOrder").innerHTML=orderDetails;
}
else{
//alert user
alert("Select at leat three toppings");
}
}
</script>
</body>
</html>
=====================================
Screen 1:
Screen 2: