Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of values from dropdown that are NOT selected using jquery

I have two related dropdowns.

When the user selects an option from the first drop down, the second drop down is populated with a list of all the options from the first drop down that were NOT selected.

I'm trying to use jquery to get all the not selected options, but I'm still a jquery newb and must be mising something.

I am trying the following:

$('#segmentCrossStreet1:not(:selected)')

This is where "segmentCrossStreet1" is the ID of the first drop down. This doesn't appear to return anything useful. What am I doing wrong?

like image 397
Amanda Kitson Avatar asked Nov 01 '11 14:11

Amanda Kitson


2 Answers

Fiddle: http://jsfiddle.net/uzhWS/ (this fiddle also shows how to pupulate another <select>)

You have to select the <option> elements, rather than the "selected <select>" elements:

$('#segmentCrossStreet1 option:not(:selected)');
like image 168
Rob W Avatar answered Nov 09 '22 03:11

Rob W


Your current selector:

$('#segmentCrossStreet1:not(:selected)')

Searches for all #segmentCrossStreet1 elements that are not selected. Is this what you want? I doubt it. Add a space before :not( to search for child elements:

$('#segmentCrossStreet1: not(:selected)')

Better if you made it more specific:

$('#segmentCrossStreet1: option:not(:selected)')
like image 33
Blender Avatar answered Nov 09 '22 05:11

Blender