Need help with js


Hi all,

I need assistance with the following code/function. So the idea is that the two values show 2 different results, one<p> for Addittion of two numbers and the other <p> for multiplication of two numbers.

<meta charset=utf-8 />
<title>JS Bin</title>
 <input id="val1" onkeyup="sumar()"><br>
 <input id="val2" onkeyup="sumar()"><br>
 <span id="Display"></span>


function sumar()
 var total = 0;
 var valor1 = document.getElementById("val1").value;
 var valor2 = document.getElementById("val2").value;

 total = (valor1 * valor2);

 var Display = document.getElementById("Display");
 Display.innerHTML = total;

So i have the function to show the result of the multiplication of the two values, but im stucked for the addition, any suggestion?

Thank you


you could rename total to total_mul and create another variable total_sum, then use innerHTML to display the result to Display? You could add paragraphs, you can concat strings and variables like so:

"<p>" + total_mul


Try using the unary operator to cast your values to numbers.

total = +valor1 * +valor2


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.