Javascript - Viết chương trình nhân, chia
Viết chương trình nhân, chia
Viết chương trình JavaScript để tính phép nhân và phép chia hai số:

Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JavaScript program to calculate multiplication and division of two numbers </title>
<script>
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 * num2;
}
function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;
}
</script>
<style type="text/css">
body {margin: 30px;}
</style>
</head>
<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
</body>
</html>
Giải thích:
document.getElementById (id) .value: Thuộc tính value gán hoặc trả về giá trị của thuộc tính giá trị của trường văn bản.
document.getElementById ("result"). innerHTM: Thuộc tính innerHTML gán hoặc trả về nội dung HTML (HTML bên trong) của một phần tử.

