Tuesday, April 28, 2009

[jQuery] Re: jQuery next() to find <select><option>

Are you able to use the keyup event instead or does it need to be
keydown?
(Inside the keyup event handler the option:selected item should be the
one you're after, instead of having to derive it.)

Failing that could you just find the next/previous option by using
selectedIndex +/-1 ?
Something along the lines of...
var prevOrNext = e.keyCode == 37 ? -1 : e.keyCode == 39 ? 1 : 0;
if( prevOrNext ){
var $list = $(this).parents("SELECT");
var selIndex = $list.attr("selectedIndex") + prevOrNext;
location.href = $list.find("OPTION").eq(selIndex).val();
}

I dare say you can slim the code down a little!

George

> problem is I am using <optgroup> too and can't find a way to make it
> work

No comments: