Solved

Multiple select menus and jquery

Posted on 2009-05-18
7
530 Views
Last Modified: 2012-05-07
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
Comment
Question by:PhotoMan2000
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 24417077
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
 

Author Comment

by:PhotoMan2000
ID: 24421936
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
 
LVL 16

Accepted Solution

by:
anoyes earned 350 total points
ID: 24421978
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:PhotoMan2000
ID: 24427347
@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
 
LVL 16

Assisted Solution

by:anoyes
anoyes earned 350 total points
ID: 24427715
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
 

Author Comment

by:PhotoMan2000
ID: 24431510
Thanks I'll try that.. does the .append also handle the closing bracket of the tags?
0
 

Author Closing Comment

by:PhotoMan2000
ID: 31582795
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question