Need help with js


#1

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.

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

 </div>
 
 
<script>

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;
 }
 
</script>
</body>
</html>

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


#2

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

#3

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

total = +valor1 * +valor2

#4

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