1

Usage:

I have a form that has a select drop-down that's controlled by another select drop-down. On loading the page (if values where selected before, PHP loads them) select drop-down #2 hides several options (this works). When selecting a particular option with select drop-down #1, select drop-down #2 hidden options become available (.show(), this works).

The problem: (An example to outline the problem: http://jsfiddle.net/RqhbY/7/)

When the hidden options are shown/available and one is selected and then the select drop-down #1 changes that value that hides the options in drop-down #2, On submission the selected hidden value gets submitted.

How can I un-select/reset the selected option that is now hidden?

2
  • Just wondering, are you working on some legacy code or something? Why the jQuery 1.3.2? Commented Dec 13, 2011 at 22:04
  • @Jasper yeah it's a older project anf there is so much older code that uses jQuery 1.3.2 that I can't upgrade it just yet. A complete rewrite is happening but that could take months and this needs to get fix ASAP Commented Dec 14, 2011 at 14:08

1 Answer 1

4

http://jsfiddle.net/RqhbY/8/

Try changing:

$('select[name=two[0]] option').attr('selected', false);

with:

$('select[name=two[0]] option').removeAttr('selected');

UPDATE

OK, I tested the above code in IE 8 and the disabled <option> is still selected. This seemed to work however:

$('select[name=two[0]]').children('option').removeAttr('selected').filter(':nth-child(1)').attr('selected', true);

Demo: http://jsfiddle.net/RqhbY/9/

Note that you may want to update the .filter() call to only select non-disabled options but I'll leave that for you.

UPDATE

If you call .attr('selectedIndex', -1) on the <select> element then you can have no <option>s selected. The drop-down will not show a value but instead will be blank.

Demo: http://jsfiddle.net/RqhbY/10/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.