What's wrong with this syntax?

I have a list of check boxes with values. Clicking each check box, displays the sum of the checked box and the MSRP value (1000) in a div - as outlined below. Can someone explain this little snippet and why it is not working? I started hacking this snippet and it is beyond meaningful… so be gentle.


<p>MSRP: 1000</p>
<ul id="ComboOneAddonPricing">
<li><label for="addon-a">Addon One<input id="addon-a" type="checkbox" value="500"></li>
<li><label for="addon-b">Addon Two<input id="addon-b" type="checkbox" value="700"></li>
</ul>

<div id="comboOneSum">1000</div>


jQuery(document).ready(function($) {
            var comboOneSum = 1000;
            $('#ComboOneAddonPricing :checkbox').click(function() {
                comboOneSum = 1000;
                $('#ComboOneAddonPricing :checkbox:checked').each(function(idx, elm) {
                    comboOneSum += parseInt(elm.value, 10);
                });
                
                $('#comboOneSum').html(comboOneSum);
            });

Hi,

It’s not working, as you have not closed the jQuery(document).ready(function(){ ... }); block

You don’t actually need this if you put your script at the end of your page:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Addition example</title>
  </head>
  
  <body>
    <p>MSRP: 1000</p>
    <ul id="ComboOneAddonPricing">
      <li><label for="addon-a">Addon One<input id="addon-a" type="checkbox" value="500"></li>
      <li><label for="addon-b">Addon Two<input id="addon-b" type="checkbox" value="700"></li>
    </ul>
 
    <div id="comboOneSum">1000</div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      var comboOneSum = 1000;
      $('#ComboOneAddonPricing :checkbox').click(function() {
        comboOneSum = 1000;
        $('#ComboOneAddonPricing :checkbox:checked').each(function(idx, elm) {
          comboOneSum += parseInt(elm.value, 10);
        });

        $('#comboOneSum').html(comboOneSum);
      });
    </script>
  </body>
</html>

Is there anything about the code which is not clear?
I’d be more than happy to explain it to you.

Thanks… can you explain this line:


comboOneSum += parseInt(elm.value, 10);

Also what is an efficient way to repeat this three times. If I have three columns that need to have this same functionality. Do i just repeat this snippet three times with unique IDs for each?

That takes the string in elm.value and converts it from base 10 to base 10 then adds the resulting number to whatever is in the comboOneSum field.

Since you are assuming already that the number is base 10 and parseInt is for converting to base 10 you don’t really need to use parseint to convert it - all you really need to do is to convert the string to a number - which is what the Number() function is for. You should only ever use parseint if the string contains a number using a different number base (2 thru 9 or 11 thru 36).

comboOneSum +=Number(elm.value);

You can make it even shorter using a unary plus operator which will also convert a string to a number.

comboOneSum += (+elm.value);

Thanks Felgall. Very helpful.

No, don’t do that. Duplicate code is a bad thing.

I’m not sure how comfortable you feel with JS, but it’s relatively simple to make the whole thing more generic.

I’ve made you a little demo of how one might accomplish this.

You can see it here.

Feel free to ask if you have any questions or would like anything explained further.

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Addition example</title>
    <style>
      fieldset { margin-bottom: 15px; }
      ul.addonPricing { margin: 0 0 15px 0; padding: 0 0 0 20px;}
    </style>
  </head>
  
  <body>
    <fieldset>
      <legend>MSRP: 1000</legend>
      <ul class="addonPricing">
        <li>
          <label for="addon-a">Addon One ($500)</label>
          <input id="addon-a" type="checkbox" value="500">
        </li>
        <li>
          <label for="addon-b">Addon Two ($700)</label>
          <input id="addon-b" type="checkbox" value="700">
        </li>
      </ul>
      <div><strong>Total:</strong> $<span class="sum" data-total="1000"></span></div>
    </fieldset>

    <fieldset>
      <legend>MSRP: 2000</legend>
      <ul class="addonPricing">
        <li>
          <label for="addon-c">Addon Three ($900)</label>
          <input id="addon-c" type="checkbox" value="900">
        </li>
        <li>
          <label for="addon-d">Addon Four ($1100)</label>
          <input id="addon-d" type="checkbox" value="1100">
        </li>
      </ul>
      <div><strong>Total:</strong> $<span class="sum" data-total="2000"></span></div>
    </fieldset>
    
    <fieldset>
      <legend>MSRP: 3000</legend>
      <ul class="addonPricing">
        <li>
          <label for="addon-e">Addon Five ($1300)</label>
          <input id="addon-e" type="checkbox" value="1300">
        </li>
        <li>
          <label for="addon-f">Addon Six ($1500)</label>
          <input id="addon-f" type="checkbox" value="1500">
        </li>
      </ul>
      <div><strong>Total:</strong> $<span class="sum" data-total="3000"></span></div>
    </fieldset>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      function updateTotal($box){
        var $parent = $box.parents('.addonPricing'),
            $boxes = $parent.find(":checkbox"),
            $sum = $parent.next().find(".sum"),
            baseValue = Number($sum.data("total")),
            newValue = 0;

        $boxes.each(function(){
          if($(this).is(":checked")){
            newValue += Number($(this).val());
          }
        });

        $sum.html(baseValue + newValue);
      }

      $(':checkbox').change(function(){
        updateTotal($(this));
      });

      $(':checkbox').trigger("change");
    </script>
  </body>
</html>

Thanks so much. I think I know enough to see what you did to make it nice and tidy. I was playing around myself to tidy it up into one snippet that could be used for all three columns. This is helpful. Appreciate the help!