Skip to content Skip to sidebar Skip to footer

How To Multiply / Sum With Javascript

https://jsbin.com/wujusajowa/1/edit?html,js,output I can sum the numbers of options. Like (5+5+5=15) But I don't know a way to multiply the input with the sum of selects. For examp

Solution 1:

Use <input type="number">, define a global variable to store value of <input>; attach change event to <input> element to update global variable; use variable at change event of <select> element if variable is defined, else use 1 as multiplier

var input = 0;

$('select').change(function(){
 var sum = 0;
    $('select :selected').each(function() {
        sum += Number($(this).val());
    });
     $("#toplam").html(sum * (input || 1));
}).change();

$("#miktar").on("change", function() {
  input = this.valueAsNumber;
});

jsbin https://jsbin.com/cujohisahi/1/edit?html,js,output

Solution 2:

Here's a simple example:

var mySum = 6 * ( 5 + 5 + 5 );
document.getElementById('result').innerHTML = mySum;
<divid="result"></div>

Post a Comment for "How To Multiply / Sum With Javascript"