jQuery: How can I set the selected attribute to a next available option element if the currently selected option is disabled?
-
The console shows Blue, which is good and now I'd like to set theĀ selected attribute to the next available (not disabled) option tagĀ element and remove the selected attribute from the current disabled one. <select id="test"> <option >Red</option> <option selected disabled="Disabled">Blue</option> <option>Yellow</option> <option disabled="Disabled">Orange</option> <option>Green</option> </select> <script type="text/javascript"> console.log( $( "select#test option:selected" ).attr( "value" ) ); </script>
-
Answer:
I think this will do: <script type="text/javascript"> var selected_option = $('#test option:selected'); if(selected_option.is(':disabled')) { selected_option.removeAttr('selected').nextAll(':not(:disabled)').first().prop('selected', true); } console.log($('#test option:selected').attr('value')); </script>
Caio Vaccaro at Quora Visit the source
Related Q & A:
- How can I set music as a ringtone on my iPhone?Best solution by Yahoo! Answers
- How can I set a wireless camera to record on my computer?Best solution by Yahoo! Answers
- How can I set up my computer to alert me when I get an incoming email?Best solution by Yahoo! Answers
- How can I set up a website for free?Best solution by Yahoo! Answers
- How can I set up 2 monitors on a pc?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.