SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict EvilDoppler's Avatar
    Join Date
    Dec 2001
    Location
    Perth, Australia
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question help! - desperate js newbie

    Hey folks,

    just thought to swing by and ask a simple JS question (for you that is!).

    I have a select box (multiple) which contains numerica values and could like to add the sum of those selected to another text field or variable.

    Could anybody help me out here ?

    cheers n' thanks
    * Due to the last tax raise our prices has risen
    * - answers w/o thought $1 - answers w/ thought $3
    * - correct answers $10
    * - Dumb stares are still free

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here you go this works nicely

    PHP Code:

    <script type="text/javascript">

      function 
    calcultor()
      {
        var 
    x    document.forms[0].elements[0]
        var 
    total0;

        for (
    a=0;a<x.length;a++)
        {
          
    total+=parseInt(x.options[a].value);
        }

        
    alert(total);

      }

    </script>

    <form id="form1">

    <select id="calc" name="calc">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
    </select>

    <input type="button" value="calculate" onclick="calcultor()" />

    </form> 

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the one above was for the value tag whereas this is for the text that gets displayed in the select statement

    PHP Code:
    <script type="text/javascript">

      function 
    calcultor()
      {
        var 
    x    document.forms[0].elements[0]
        var 
    total0;

        for (
    a=0;a<x.length;a++)
        {
          
    total+=parseInt(x.options[a].innerHTML);
        }

        
    alert(total);

      }

    </script>

    <form id="form1">

    <select id="calc" name="calc">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
    </select>

    <input type="button" value="calculate" onclick="calcultor()" />

    </form> 

  4. #4
    SitePoint Addict EvilDoppler's Avatar
    Join Date
    Dec 2001
    Location
    Perth, Australia
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question thanks - but...

    Thanks for your help andrew, much appreciated....

    but...

    (this is the bad part)..

    The scripts seems to sum 45 regardless of what you select, so that it actually ads up all the figures in the select box.

    (1+2+3+4+5+6+7+8+9=45)

    so it almost does what it's supposed to, though not excactly the expected/wanted outcome.

    I'll work with the script you've given me and see if i can't get my outcome to appear eventually.

    cheers
    * Due to the last tax raise our prices has risen
    * - answers w/o thought $1 - answers w/ thought $3
    * - correct answers $10
    * - Dumb stares are still free

  5. #5
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work

    (even though i'm sure Andrew probably has a better way )

    PHP Code:
    <script type="text/javascript">

      function 
    calcultor()
      {
        var 
    x     document.forms[0].elements[0]  // Form Value
        
    var total 0                              // Total Values Selected

        // Loop through selections while adding them
        
    for (0x.lengtha++)
        {
             
    // Check if option is selected or not
         
    if (x.options[a].selected)
          
    // Add number to total
          
    total += parseInt(x.options[a].innerHTML);
         else {}
        }
        
    // Display Total
        
    alert(total);

      }

    </script>

    <form id="form1">

     <select id="calc" name="calc" size="9" multiple>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
     </select>

    <input type="button" value="calculate" onclick="calcultor()" />

    </form> 

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh right, thats what he meant lol, i thought he want the total for the box lol. thats works fine platinum nice1

  7. #7
    SitePoint Addict EvilDoppler's Avatar
    Join Date
    Dec 2001
    Location
    Perth, Australia
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Thanks heaps folks

    Hey - thanks folks, the helps been falling on a dry spot today. The problem was running me ragard and I just didn't seem to be able to get around it.

    Anyways, your solutions helped and i'm now on the move again (gawd i do hate JS!!)

    cheers and thanks again
    * Due to the last tax raise our prices has risen
    * - answers w/o thought $1 - answers w/ thought $3
    * - correct answers $10
    * - Dumb stares are still free


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
  •