1

I have written a jquery script. It concatenates instead of adding two values

$(document).on('click','.woocommerce-checkout-review-order input[type="submit"]', function(){
   var sum = '';
   $('.checkout_ilm select').each(function(){
      if(jQuery(this).val()!=''){
         sum += Number(jQuery(this).val());  // Or this.innerHTML, this.innerText
      }
   });
   alert(sum);
});

When the values in all the select fields is zero then it concatenates the value instead of adding it. So in this case it gives 00 instead of 0 as a result. I have used parseInt and parseFloat also both working but this conditions returns true with parseInt and parseFloat even sum is equal to zero and ilm_selected has also value zero.

if(sum < ilm_selected || sum==''){
   //some code
}

Please help, Thanks in advance.

4
  • use parseInt(jQuery(this).val()); Commented Aug 25, 2017 at 6:20
  • you should not use parseInt(). Check my answer for more info. Commented Aug 25, 2017 at 6:27
  • Change your var sum = ''; to var sum = 0;. This should work for you. Commented Aug 25, 2017 at 6:36
  • @Rizwi , this solution is helpful to you? Commented Aug 26, 2017 at 10:24

4 Answers 4

2

I just replace Number with parseInt, try this solution this will help you.

Refer this link for more information:click here

jQuery(document).on('click','.woocommerce-checkout-review-order input[type="submit"]', function(){
    var sum = 0;
    jQuery('.checkout_ilm select').each(function(){
             if(jQuery(this).val()!=''){
             sum += parseInt(jQuery(this).val()); 
        }
    });

    alert(sum);
});
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use parseInt(). The parseInt() function parses a string and returns an integer.

sum += parseInt(jQuery(this).val());  // Or this.innerHTML, this.innerText

Comments

1

Try this -

jQuery(document).on('click','.woocommerce-checkout-review-order input[type="submit"]', function(){
  var sum = 0;
  jQuery('.checkout_ilm select').each(function(){
    if(jQuery(this).val()!=''){
      sum += parseFloat(jQuery(this).val());
    }
  });
  console.log("Sum: ", sum);
});

You can use parseInt() too, but if your value contains decimal for example 20.50 than it will not work, so it is better to go for parseFloat().

Comments

0

You have to change var sum = ''; to var sum = 0;.

This will help you in getting the sum of all the select box.

Your earlier code was concatenating value of select box which you converted into number to string as sum was initialised to ''.

Working Demo

$(document).on('click','input[type="submit"]', function(){
  var sum = 0;
  $('select').each(function(){
      if($(this).val() !== ''){
        sum += Number($(this).val()); 
  }
});
console.log(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value="1">Volvo</option>
  <option value="20">Saab</option>
  <option value="34">Mercedes</option>
  <option value="7">Audi</option>
</select>
<select>
  <option value="1">Volvo</option>
  <option value="20">Saab</option>
  <option value="34">Mercedes</option>
  <option value="7">Audi</option>
</select>
<select>
  <option value="1">Volvo</option>
  <option value="20">Saab</option>
  <option value="34">Mercedes</option>
  <option value="7">Audi</option>
</select>

<input type="submit" value="Submit">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.