multiply two numbers in javascript code example

Example 1: javascript how to multiply numbers

<!-- Multiplying two numbers in HTML and Javascript -->

<main>
<label for="firstNum">Number 1:</label>
<input type="number" id="firstNum" name="firstNum">

<label for="secondNum">Number 2:</label>
<input type="number" id="secondNum" name="secondNum"></br></br>

<button onclick="multiply()">Multiply</button></br></br>

<label for="result">Result</label>
<input type="number" id="result" name="result"/>
</main>

<script>
function multiply(){
	num1 = document.getElementById("firstNum").value;
	num2 = document.getElementById("secondNum").value;
	result = num1 * num2;
	document.getElementById("result").value = result;
}
</script>

Example 2: calculate two number and diplay next field without reload the page javascript

var text1 = document.getElementById("Text1");
var text2 = document.getElementById("Text2");

function add_number() {
   var first_number = parseFloat(text1.value);
   if (isNaN(first_number)) first_number = 0;
   var second_number = parseFloat(text2.value);
   if (isNaN(second_number)) second_number = 0;
   var result = first_number + second_number;
   document.getElementById("txtresult").value = result;
}

Tags:

Html Example