In the html code I have select options like this:
<option value="1" class="myclass5">Value1</option>
In jQuery:
var cod = $(this).val(); // This is the value of another select: when the value
$("option[class^=myclass]").hide();
$("option[class^=myclass]").each(function () {
$("option[class^=myclass" + cod + "]").show();
});
EDIT
I have two select. When I select a value in the first select, the second one must be populated accordingly (I have to prevent an ajax call).
I put all the second select values in a session var, but my problem is in selecting only those ones tied to the first select, so I was trying through css classes.
EDIT2
<select name="firstselect">
<option value="0" selected="selected">Choose...</option>
<option value="1">Value1</option>
<option value="2">Value2</option>
<option value="3">Value3</option>
</select>
<select name="secondselect">
<option value="0" selected="selected">Choose...</option>
<option value="myclass1">Value11</option>
<option value="myclass1">Value12</option>
<option value="myclass1">Value13</option>
<option value="myclass2">Value21</option>
<option value="myclass2">Value22</option>
<option value="myclass2">Value23</option>
<option value="myclass3">Value31</option>
<option value="myclass3">Value32</option>
<option value="myclass3">Value33</option>
</select>
EDIT3
For me greenish's solution is good, but there is an issue on IE that I don't succeed in explaining: when I use the back button, the user selected value is "lost", that is, if I log on the console the user selected value, I see its "index" in the new cloned select. In the html source code, there is the whole original select.
EDIT4
I resolved thanks to this post
I'm not sure I completely understand your question but what about something like this:
http://jsfiddle.net/wVCcH/1/
Alternatively, the dynamic writing option also exists:
http://jsfiddle.net/UNy9Z/4/
Include a javascript object that has a mapping of selections
this type of question comes up often in SPA development, and I've developed this approach. What I recommend is creating a javascript object with the different option lists, and object keys consisting of the names of the select fields which will cause sibling:after select fields to be dynamically created, by binding to the 'select' event of the dependent select element, and running a custom script to evaluate the selected option, parse your map, and then render options accordingly.
I've taken this concept, and created a pretty nifty shell, that should not only allow you to this between the two select elements you mention, but essentially allowing you to have as many depending select elements as you want... populating all of them based on the selection made in the dependent element. Here are the even cooler advantages:
$.extend()
and the providedSelectOption
objectmap
object by calling custom functionsselect
event. Allowing you to use non-form elements and inject dropdown type functionality and still affect other elements (**works with Bootstrap!I have created a gist to demonstrate the concepts. I have the initial shell listed here, but check the link to ensure you have the most updated version and information on this gist. (I may turn it into a plugin)
https://gist.github.com/LongLiveCHIEF/7880119.js
form.html
selection-map.js
So when I understand your question correctly, you want to make the options of the second select field dependent of the selected value in the first select field.
I tried this quickly and hiding an option with css does not seem to work cross browser, even the latest version of chrome on mac won't hide the options.
So I came up with the following:
HTML first: I used classes to mark the dependencies. This allows the
value
attribute in the second select field to be used without restrictions.Also, only options marked as
conditional
will be changed, the others won't be affected. This is useful for something like the Default value in this scenario.And the Javascript: To make this work, I copied the options of the
secondSelect
field and saved them in a variable. This allows me to actuallyremove
options from the select field while I'm still able to retrieve the options from the copy.And here's a fiddle that shows the script in action: http://jsfiddle.net/Kn8Gc/
Note: If you get Data form a database and the user already selected #firstSelect, just use the
selected="selected"
attribute. #secondSelect will automatically display the correct values. Just try the fiddle above and "preselect" for example value1 instead of 0!Also here's a "generic" function that can be used easily to make any two select fields dependent on eachother (still using the classes
conditional
+source value
to make the relation):And here it is in action: http://jsfiddle.net/NUxQ9/
Hiding an option is not defined in all browser. It is not defined in the W3C specification.
http://www.w3.org/TR/html401/interact/forms.html#h-17.6
Disabling the option based on first select value
You can disable the second option which will not allow the user to select the option.
DEMO: http://jsfiddle.net/HrpF2/
Code:
Show/Hide options based on first select value
If you really want to show/hide options, then you need to clone the second options and add them back based on the first select value. See below,
DEMO: http://jsfiddle.net/HrpF2/1/
Code:
The only way I got this to work across browsers was to detach and then re-attach the options. I added a class to each option to also break the value of the option apart from its grouping. These two things may be related, but I won't assume that.
and the Javascript...
Fiddle: http://jsfiddle.net/JbVWV/8/
Edit - I added the call to
$("#firstselected").trigger("change")
on page load. So, if there is something selected in the first dropdown already, the second dropdown will already be populated correctly.