views:

1822

answers:

3

Hi i need set focus to the element that is immediate next(and in other case immediate prev ) to the current element

eg

<li>item1</li> <- prev element (get this element)
<li>item1</li><- current element
<li>item1</li> <- next element (get this element)
<li>item1</li>

this is what i used

             var curr = (event.target).next();
              $(curr).trigger('focus');

when i check the value of curr in firebug it shows undefined ??

A: 
var curr = (event.target).nextSibling;
Yossarian
A: 

Are you inside a jquery event handler? If so, why not use $(this).next() and/or $(this).prev() ?

Dan F
and what about event bubbling?
geowa4
Fair question. It would depend on the original selector used to hook the event, which was missing from the question. Nice answer btw, +1 from me
Dan F
+3  A: 

Use

$(event.target).next()

to get the next sibling. You can also pass an expression to the next function. This will select the next sibling to match you selector:

$(event.target).next("p")

You can also use nextAll, which works the same way but returns all of the following sublings. See more here. There is also, prev and prevAll, which are the analogues for getting the previous element(s).

Basically, you were really close, you just need to wrap event.target in the jQuery object, as event.target returns the actual element.

geowa4