How does one select, or focus, a <select>
box using javascript? .select()
works fine for selecting inputs with jQuery, but it does nothing for select boxes. Trying to select the currently selected option doesn't work either.
Update: People are misunderstanding the question two ways.
First this is not a duplicate of how to select a text input. As I already stated, the usual method that works for selecting a text box does not work.
I am not trying to retrieve or set the selected index of a select box. I just want the select box in general to be selected, like when you are tabbing through a form.
Update 2:
OK, I figured out what the problem was which makes the question moot. Due to a quirk of testing from the Chrome console, .focus()
did nothing, which made me believe it was useless. I guess that is why I had ended up using .select()
which visibly worked when I tested from the Chrome console, but didn't work for select boxes. When I tried .focus()
from a javascript file then it worked. I guess when the Chrome console is in focus then you can't focus stuff with in the actual page programatically. Lesson learned.