A company sells five different products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49; and product 5, $6.87. Write a JavaScript code for the below:
Make use of a prompt dialog to obtain the product number, quantity and price from the user. Displays the total price of all the products.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type = "text/javascript">
function getVal() {
var prodVal = prompt("Enter product number : ", "");
var quanVal = prompt("Enter the quantity : ", "");
var priceVal = prompt("Enter price : ", "");
var total = quanVal*priceVal
document.getElementById("area").innerHTML = ("Product number is : " + prodVal + "<br>Quantity is : " + quanVal + "<br>Price is : " + priceVal + "<br>The total price of all products is: " + total);
}
</script>
</head>
<body>
<table>
<th>Product number</th>
<th>Product price</th>
<tr>
<td>Product 1</td>
<td>$2.98</td>
</tr>
<tr>
<td>Product 2</td>
<td>$4.50</td>
</tr>
<tr>
<td>Product 3</td>
<td>$9.98</td>
</tr>
<tr>
<td>Product 4</td>
<td>$4.49</td>
</tr>
<tr>
<td>Product 5</td>
<td>$6.87</td>
</tr>
</table>
<br>
<form>
<input type = "button" value = "Click to input" onclick = "getVal();" />
</form>
<div id="area">
</div>
</body>
</html>
Code screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.