Solved

Multiple select menus and jquery

Posted on 2009-05-18
7
531 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)

860 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