Link to home
Start Free TrialLog in
Avatar of PhotoMan2000
PhotoMan2000

asked on

Multiple select menus and jquery

I have 5 drop down selects menus each with 5 items. (Using 2 for the example below to save space)

I need to pass the value fo the unique ID to a jquery script to display the item's value inside a div.

PS
This line of code for the var 'output' in the sample is missing the plus sign for some reason.


<script>  
 $(document).ready(function(){
     $("#developer").change(onSelectChange);         
 });  
 function onSelectChange(){
	//var theSelecteMenu = $(this).options[this.selectedIndex).value;
    var selected = $("#developer option:selected");       
    var output = "";  
     if(selected.val() != 0){  
         //output = "You Selected " + selected.val();  
		 output = "includes/video.cfm?videoID="+selected.val();
		 }
     $("#outputarea").html(output);  
 }   
     </script>            
<select id="developer1" onChange="onSelectChange();">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select>
<select id="developer1" onChange="onSelectChange();">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select> 
 <div id="outputarea"></div>

Open in new window

Avatar of anoyes
anoyes
Flag of United States of America image

I tweaked your script a little - take a look.  For starters, I got rid of the inline javascript event handlers (onChange...) and did it in the document.ready function.  I'm selecting all select elements whose IDs start with developer, and executing the onSelectChange function when that dropdown changes.  Then, in your onSelectChange, $(this) refers to the dropdown element that changed, so you can do $(this).find('option:selected') to get the selection option.  Let me know if you need further clarification or if this doesn't work for you.

<script>  
 $(document).ready(function(){
     $("select[id^='developer']").change(onSelectChange);         
 });  
 function onSelectChange(){
    var selected = $(this).find('option:selected');
    var output = "";  
     if(selected.val() != 0){    
       output = "includes/video.cfm?videoID="+selected.val();
     }
     $("#outputarea").html(output);  
 }   
</script>            
<select id="developer0">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select>
<select id="developer1">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select> 
 <div id="outputarea"></div>

Open in new window

Avatar of PhotoMan2000
PhotoMan2000

ASKER

Thanks, but when I tied your updated code the 2nd select (id="developer1') - didn't function, only developer0 worked.

I need it to know which select menu is being used, as there will be more in the final use.

I need to have multiple selects with the same options, as each selected item will eventually be saved to a database.

Thanks.


ASKER CERTIFIED SOLUTION
Avatar of anoyes
anoyes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@anoyes - yes that worked.  I now need to expand upon this a bit more .. I can either open a new question or increase the points and award you the 350 up to this point, plus the rest if you supply the answer.

So, here is what  I need.

Using the same selects. Instead of writing to the same div, I need to be able to write out to their own div. Soft of like as each item is selected (developer0, apple) a hytper link appears on the page. When the 2nd is selected a NEW div (developer1, Orange) appears.  - Like creating a shopping list if items, from a series of select items, but as hyper links. (Does that make sense??)


Some code is below.  
<select id="developer0">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select>
<select id="developer1">  
     <option value="0">Select Developer</option>  
     <option value="1">APPLE</option>  
    <option value="2">ORANGE</option>  
     <option value="3">GRAPE</option>  
 </select> 
 <div id="outputarea"></div>
 
<!-- sample of what is expected to be output if both above are selected --->
<div><a href="foo.com?linkid=developer0-1">Link to Video</a></div>
<div><a href="foo.com?linkid=developer1-2">Link to Video</a></div>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks I'll try that.. does the .append also handle the closing bracket of the tags?
Thanks! The code worked - though due to changes in my UI - I had to abandon this method.  I do have another jquery issue (yet similar) , and will post a new question.