SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting the sum of all checkboxes

    Hi guys,

    I need a function that will poll a list of checkboxes of the same name. Each checkbox has a numeric value and I need to get the sum of them all and return 0 if there's none selected. Basically this is a list of dollar value accessories that will be added to final price. The final price value will poll for this value and add it to itself. Any hints?

    Aaron
    Aaron Brazell
    Technosailor



  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    This should do the trick. It may need some further optimizing, but it should work.

    Code:
    function getTotal (formname, elementname) {
    //current price variable
    var totalPrice = 0;
    //variable for current element
    var currentEl;
    //variable for number of checkboxes selected
    var numClicked = 0;
      for (var i = 0; i < formname.elements.length; i++) {
      //get current element and store into variable
       currentEl = formname.elements[i];
        if (currentEl.name == elementname && currentEl.checked) {
          //add price
          totalPrice += parseFloat(currentEl.value);
          //add to number of boxes clicked
          numClicked ++;
        }
      }
      if (numClicked > 0) { //one or more boxes checked
        return totalPrice; 
      } else return 0; //no boxes checked
    }

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What will trigger the function? A button press, or some other event?

    I'd do it this way (notice the more semantic function name)
    Code:
    function totalCheckboxes( formname, elementname )
    {
    	var group = document.forms[formname].elements[elementname],
    		totalPrice = 0,
    		i = 0,
    		cb;
    	while( cb = group[i++] )
    	{
    		if ( cb.checked )
    		{
    			totalPrice += parseInt( cb.value, 10 );
    		}
    	}
    	return totalPrice;
    }
    Vinnie, your function is good, but notice how the numClicked variable is unecessary, since totalPrice is initialized at 0?

    A note about checkbox arrays - whether whomever is reading already knows or not: Retrieving a reference to the element by its name will return an array (assuming there's two or more) - that array being an array of references to each checkbox.
    Last edited by beetle; Sep 5, 2003 at 21:13.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because people tend to check and then uncheck when they see the price:

    Code:
    totalPrice += ( cb.checked ) ?
    parseInt( cb.value, 10 ) :
    - (parseInt( cb.value, 10 ) );


    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, and that primarily depends on what event(s) trigger the function.

    That being said, I couldn't agree more.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Happy Birthday, Sketch!

    (I'm getting the info from the bottom of the page at the: webXpertz Forums

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •