• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

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

0
PhotoMan2000
Asked:
PhotoMan2000
  • 4
  • 3
2 Solutions
 
anoyesCommented:
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

0
 
PhotoMan2000Author Commented:
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.


0
 
anoyesCommented:
You can get the ID of the dropdown that was changed using $(this).attr('id') in the onSelectChange function (example below).  Both dropdowns function for me - does your document.ready function match mine?  Note the ^=.

function onSelectChange(){
    var id = $(this).attr('id');
    var selected = $(this).find('option:selected');
    var output = "";  
     if(selected.val() != 0){    
       output = "includes/video.cfm?videoID="+selected.val();
       alert(id);
     }
     $("#outputarea").html(output);  
 } 

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PhotoMan2000Author Commented:
@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

0
 
anoyesCommented:
It's really easy to create HTML elements with jQuery by just doing $('<element>').  So, if you wanted to add a <div> and <a> element to the page with each selection you could do something like this...

 function onSelectChange(){
    var id = $(this).attr('id');
    var selected = $(this).find('option:selected');  
     if(selected.val() != 0){    
       href = "includes/video.cfm?videoID="+selected.val();
       $('#outputarea').append($('<div>').append($('<a>').attr('href',href).html(selected.html())));
     } 
 }  

Open in new window

0
 
PhotoMan2000Author Commented:
Thanks I'll try that.. does the .append also handle the closing bracket of the tags?
0
 
PhotoMan2000Author Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now