Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple select menus and jquery

Posted on 2009-05-18
7
Medium Priority
?
561 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 1400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

971 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