Efe Öztürk Efe Öztürk - 1 year ago 70
Javascript Question

How to Multiply / Sum with Javascript


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 example, What should I do to do 6 x (5+5+5) and get 90 ?

Answer Source

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;

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

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download