Next jquery option

trying to select the next drop-down list of options using an identifier identifier, but to no avail

here is the code

$('#chapter option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected') 
+4
source share
2 answers

I assume that the #chapter is a <select> or one of its ancestors.

If this is the case, remove the context argument.

 $('#chapter option:selected').removeAttr('selected') .next('option').attr('selected', 'selected'); 

How you did it, you really did it:

 $('select').find('#chapter option:selected').removeAttr(... 

... that searches for an element inside <select> that has option:selected , descending from the element with the identifier chapter .


EDIT: This answer focused only on why the choice didn't work. It would be better to complete the final task using the method in @Andy E answer .

I would recommend this as an accepted answer.

+6
source

You do not need to remove the selected attribute from the <option> element when you add it to one of your siblings, unless the <select> element has a multiple attribute.

Assuming patrick dw's answer is correct (and this is because you accepted it), you can reorganize this code into something more efficient by returning the DOM back to its roots:

 $("#chapter")[0].selectedIndex++; 

Not to mention the precious bytes that you save with the shorter code.

+5
source

All Articles