onchange without losing focus?

tpk picture tpk · Sep 2, 2009 · Viewed 13.7k times · Source

I'm creating a form with a select dropdown. One of the options is "other - please specify", which is supposed to display an extra text field for more details.

I managed to do it using onChange event + some simple value check (as I can't rely on the position).

I started testing it and realized that while it works perfectly when using a mouse (onChange is ran after the control loses focus), it doesn't when I use the keyboard (since it didn't lose focus yet) - only after I press tab do the changes appear (which looks weird).

It seems to me like I'm missing something obvious, I looked for other events and the closest I've found is onclick, but that's not it either.

So the question is, is there a better way of solving this?

Answer

You can simply add this to the <select> tag:

onkeyup="this.onchange();"