SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looping through multiple select drop down

    Hi,
    the below code, loops through all the selects on my form and if the control id matches runs the code in the inner loop,
    the way that Im thinking is that when it finds the first match it should drop into the inner loop, do its thing and then drop out and back into the outer loop and repeat, however what its doing is on the first match, its droping into the second loop and running for 4 loops (which is the number of selects matching that name).

    any suggestions.

    Code JavaScript:
    var i, count = document.getElementsByTagName("select");
    		var countElm = 0;
    		var holdTotal;
    		var totalCost =  parseFloat(document.getElementById('originalCost').value);
    		for(i = 0; i < count.length; i++)
    			{
    				if(count[i].id == 'extras[]')
    					{
    						var temp = id.options[id.selectedIndex].value;
    						alert(temp);
    						var temp1 = temp.split("|");
    						var howMany = temp1[0];
    						var itemCost = parseFloat(document.getElementById(itemName).value);
    						totalCost = (howMany*itemCost)+totalCost;						
    						countElm++;
    					}
     
    			}

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I only see one loop in in the above code, am I missing something?

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    I only see one loop in in the above code, am I missing something?
    yes, your quite correct there is only one loop there. what I should have stated was that on each loop it checks to see if the control name is equal to 'extras[]' and if it is then process the if block. what seems to happen is it gets stuck on which ever control its called from.

    this is the full code
    Code JavaScript:
    function addExtrasCost(id,curr,itemName)
    	{
     
    		var i, count = document.getElementsByTagName("select");
    		var countElm = 0;
    		var holdTotal;
    		var totalCost =  parseFloat(document.getElementById('originalCost').value);
    		for(i = 0; i < count.length; i++)
    			{
    				if(count[i].id == 'extras[]')
    					{
    						var temp = id.options[id.selectedIndex].value;
    						alert(temp);
    						var temp1 = temp.split("|");
    						var howMany = temp1[0];
    						var itemCost = parseFloat(document.getElementById(itemName).value);
    						totalCost = (howMany*itemCost)+totalCost;						
    						countElm++;
    					}
     
    			}
    	}

    and i call it with
    <select name="extras[]" class="text" id="extras[]" onChange="addExtrasCost(this,'&pound;','Golf Clubs');">

    now I can have multiple selects all named the same and what I want to do is loop though each select and get the currently selected value.

    thanks
    chris

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrisio View Post
    Code:
     var temp = id.options[id.selectedIndex].value;
    Where is id defined and (bites lip) what does the console say?
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Where is id defined and (bites lip) what does the console say?

    Ok, as soon as you asked that question I spotted whats wrong. ID is defined at the top of the function and its passed to ID from the calling trigger.
    thus
    onChange="addExtrasCost(this,'&pound;','Booster Seat');"

    because im passing the name of the control its always going to use that one as its constally being passed.

    so my question remains, how can I loop through all dropdown boxes on the form with the name 'extras[]' and for now, just 'alert()' me.

    I can get the loop going no problem but how do I check for the name of the control?

    I get no errors or warning in firebug or the firefox console either

    Thanks
    Chris

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrisio View Post
    Ok, as soon as you asked that question I spotted whats wrong. ID is defined at the top of the function and its passed to ID from the calling trigger.
    thus
    onChange="addExtrasCost(this,'&pound;','Booster Seat');"

    because im passing the name of the control its always going to use that one as its constally being passed.

    so my question remains, how can I loop through all dropdown boxes on the form with the name 'extras[]' and for now, just 'alert()' me.
    I didn't see that, but if you're using duplicate IDs as well as names, I suspect that's where the problem lies - duplicate IDs aren't allowed and could cause a conflict if used.
    Tab-indentation is a crime against humanity.


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
  •