Now that I am working a lot more with jQuery I thought I would start posting little examples of why I enjoy using it so much. If you are looking for a full on beginner or selective tutorial you should head over to the tutorials section on the jQuery site, it is packed with great stuff. This first example is a short one but to me is the main reason I have been using jQuery so much lately. Just keep in mind when I am going through these examples that I am no JS expert so there may be better ways to accomplish the task at hand.
Everyone has dealt with a select box (drop down) at one point or another right? One thing I really hate about JS is how much code I have to write just to accomplish what should be a simple task. Here is the way I would of done it in the past. In our example we have a list of developers in a select menu. We explicitly set an onChange event handler (not a good practice more on that another time) that calls our onSelectChange method. The problem now is that you have to grab the element from the dom, find the selected index and write all this code to get the values and text. I just don't have fun writing this and you can probably see why. If there is a better way that is my fault and while this would work its just no fun for me.
Now on to the fun part. The first thing we need to do is include the jquery library and create a ready function. We also have our list of developers and our goal is to write out what developer the user selects every time the selection changes.
The first thing we need to do is to notify our application that the user changed their selection. To do that we can use a jquery change event. To use this event we must first have a jquery object. This is really where the beauty of the code lies, almost everything is returned as a jQuery object. By selecting our developer select we simply attach an event and pass in the name of the function we would like to call when the event occurs.
Now we can write our method that handles the actual logic of our application. Remember all that code we wrote earlier, it was ugly right? This to me is a better approach, first you can get the selected option using the option:selected selector. This will return to us a jQuery object and we can get what we need by using the val() and text() attributes. The html() method in this case sets the contents of an element.
There is a link below for a live demo of this. This was just a quick example of why I enjoy using jQuery. The code is clean and I am never guessing what some method returned to me (object/string/date) because I will almost always get back a jQuery object. If you have any questions about this example let me know and for you more advanced users I got some good stuff in the pipe so stay tuned.
Select Demo

#1 by Daniel Kim on 7/1/08 - 6:19 AM
It's amazing how succinct jQuery allows client side code to be. Not as clear as your example (the following is probably more terse than succinct), but it's amazing that this kind of code can be written with such brevity with jQuery:
$(function(){
$("#developer").change(function() {
$("#output").text( this.value != 0 ? "You Selected " + $(this).find(":selected").text() : '' );
});
});
#2 by Raymond Camden on 7/1/08 - 7:30 AM
$("##issuetype_filter").val()
It returns the value. I don't have to get the selected property at all.
#3 by Dan Vega on 7/1/08 - 7:40 AM
#4 by Raymond Camden on 7/1/08 - 7:41 AM
console.log($("#foo").val());
console.log($("#foo option:selected").text());
In most cases folks will just want the val though and not needing to specify selected will make the code a bit slimmer.
#5 by Raymond Camden on 7/1/08 - 8:16 AM
#6 by Dan Vega on 7/1/08 - 8:20 AM
#7 by Brian Swartzfager on 7/1/08 - 8:22 AM
I also use a jQuery plugin on that same page that lets you easily add, remove, and sort the options in a select box (http://www.texotela.co.uk/code/jquery/select/). So when the student selects a mentor from the select box, that option is removed, and when the student clicks on a mentor in their existing list of mentors, an option for that mentor is added to the select box and the options are re-sorted so that the mentors remain in alphabetical order (and the AJAX calls work behind the scenes to keep the database up-to-date).
#8 by todd sharp on 7/1/08 - 9:16 AM
#9 by Dan Vega on 7/1/08 - 9:21 AM
@Todd - You would think that declaring the variable private would make it safe but I will stay on the look out.
#10 by Ben Nadel on 7/1/08 - 10:17 AM
I didn't know that about the multi-select either. Very cool! jQuery val() is sweeet.
#11 by todd sharp on 7/1/08 - 11:14 AM
Kidding (kinda) - but as I said it may be just me being anal but I avoid that kind of thing...
#12 by todd sharp on 7/1/08 - 11:17 AM
#13 by Felipe Serrano on 7/2/08 - 4:34 PM
Thanks guys
#14 by Dan Vega on 7/2/08 - 4:36 PM
#15 by Felipe Serrano on 7/3/08 - 2:42 AM
Using:
http://www.msxhost.com/jquery/linked-selects/json/...
JSCallingFunction:
$('#firstselect').linkedSelect('/cfcs/qryComponent.cfc?method=methodName','#secondselect',{firstOption: 'Search all'});
Note: "str" is passed automatically
CFFunction (CF8):
<cffunction name="methodName" access="remote" returnformat="json" returntype="array" output="false">
<cfargument name="str" default="">
<cfquery datasource="#application.dsn#" name="qryname" >
SELECT *
FROM table
WHERE ID = '#arguments.str#'
</cfquery>
<cfscript>cfcstruct = ArrayNew(1);</cfscript>
<cfloop query="qryname">
<cfscript>
cfcstruct[currentRow]['optionValue'] = val_id;
cfcstruct[currentRow]['optionDisplay'] = val_text;
</cfscript>
</cfloop>
<cfreturn cfcstruct>
</cffunction>
Thanks guys!
#16 by Dan Vega on 7/3/08 - 1:42 PM
#17 by Brad on 7/10/08 - 2:18 PM
The dropdown is populated dynamically when the user changes another value in the jqgrid.
I can't get the selected value is the issue.
I can get the desired cell with the html using html(). (<SELECT name=subJList Enabled="true"><OPTION selected>Main</OPTION><OPTION>Support</OPTION><OPTION>Support Management</OPTION><OPTION>Development</OPTION><OPTION>Co-Op Development</OPTION></SELECT>)
I've tried to create a new element and set the above code as the html then retreive the selected value.
Using .text() returns all the values in the dropdown.
i'm out of ideas except to parse the string but I assumed there's a better way.
Thanks
#18 by Dan Vega on 7/10/08 - 2:25 PM
#19 by Michael Everitt on 12/12/08 - 8:34 PM
#20 by Daniel on 12/29/08 - 8:49 PM
<select onchange="if(this.options[selectedIndex].text!='Select 1')document.getElementById('textbox').innerHTML=this.options[selectedIndex].text;" >
<option>Select 1</option>
<option>test1</option>
<option>test2</option>
<option>test3</option>
</select>
<div id="textbox">
</div>
#21 by Daniel on 12/30/08 - 2:46 AM
If the selected option's text does not equal the first selection it does not change the text, otherwise it will change the text for whatever you specify when calling the function.
<html>
<head>
<script type="text/javascript">
function selectchange(x,y)
{
if(x.options[x.selectedIndex].text!=x.options['0'].text)document.getElementById(y).innerHTML=x.options[x.selectedIndex].text;
}
</script>
</head>
<body>
<select id="selected" onchange="selectchange(this,'textbox');">
<option>Select 1</option>
<option>test1</option>
<option>test2</option>
<option>test3</option>
</select>
<div name="textbox" id="textbox">
</div>
</body>
</html>
#22 by Dan Vega on 12/30/08 - 7:19 AM
#23 by russell on 4/16/09 - 11:17 AM
#24 by Chris on 5/20/09 - 7:09 AM
http://code.google.com/p/jquery-form-extensions/
It has methods such as (plus many more):
- isRadioBox
- isChecked
- isSelected
- selectedValue
- selectedValues()
e.g. $("#myradio").selectedValue() would find the selected value from all the radio options.
#25 by Dan Vega on 5/20/09 - 8:46 AM
#26 by Brian Seagraves on 6/17/09 - 1:53 AM
#27 by John Lanz on 7/1/09 - 3:07 AM
I've been looking this selector all day long:
var selected = $("#developer option:selected");
#28 by Maicon on 9/5/09 - 2:34 AM
how to chage the selected item using jquery?
#29 by stexas on 10/8/09 - 6:53 AM
#30 by Sebastiaan on 10/28/09 - 6:07 PM
#31 by Chris on 12/15/09 - 12:52 PM
Maybe this?
http://selectswap.riaforge.org/
#32 by Sebastiaan on 12/16/09 - 2:36 AM
#33 by Silicus on 1/20/10 - 5:00 AM
#34 by Dan Vega on 1/21/10 - 1:57 PM
#35 by Sebastiaan on 1/22/10 - 4:39 AM
#36 by Kranti on 2/26/10 - 4:58 AM
#37 by Elaine on 6/4/10 - 9:03 AM
#38 by Shelton on 8/21/10 - 6:39 AM
this is exactly what i'm looking for, but as i'm a complete newbie to jquery, or js in general i'm stuck...
i need to render the output into a hidden value, because i want to send the selected option into paypal as the product title.... and obviously when i stick my code into value="" then it messes up...
anyone able to help me out? :-\
thanks alot
Shelton
p.s. the XX's are where i need it to render:
<input type="hidden" name="item_name" value="XX Orders to MyCOMPANY">
#39 by dujovski on 11/11/10 - 3:56 AM
#40 by Mike Bosch on 12/16/10 - 11:24 PM
#41 by Chris on 4/6/11 - 1:13 PM
Thank you for sharing.
#42 by Gordon on 8/4/11 - 12:51 PM
alert($(this).find(":selected").attr("selectedIndex"));
yielding "undefined"?
#43 by Gordon on 8/4/11 - 1:00 PM
$(this).find(":selected").attr("selectedIndex");
comes up undefined, jQuery this object as DOM object works:
$("#tblUAssgn select[id^='selMain']").change(function(){
alert(this.selectedIndex);
});
#44 by johnny on 2/2/12 - 9:12 AM