SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    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.

    Code HTML4Strict:
    <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>
    Code JavaScript:
    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);
                });

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code:
    <!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.
    Last edited by Pullo; Jan 4, 2014 at 05:46. Reason: Corrected code

  3. #3
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thanks.. can you explain this line:
    Code:
    comboOneSum += parseInt(elm.value, 10);

  4. #4
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by conradical View Post
    Thanks.. can you explain this line:
    Code:
    comboOneSum += parseInt(elm.value, 10);
    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).

    Code:
    comboOneSum +=Number(elm.value);
    You can make it even shorter using a unary plus operator which will also convert a string to a number.

    Code:
    comboOneSum += (+elm.value);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thanks Felgall. Very helpful.

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by conradical View Post
    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?
    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.

    Code:
    <!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>

  8. #8
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,354
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •