Skip to content Skip to sidebar Skip to footer

Select Sibling Before An Element In Jquery?

The DOM looks like this:
A
B
C
D
E
<

Solution 1:

Simplest way is using prev()

$("div#selected").prev();

According to the docs, prev() will

Get the immediately preceding sibling...

Solution 2:

.prev();

is what you need, see https://api.jquery.com/prev/

Solution 3:

To access previous sibling:

$("div#selected").prev();

To access all previous sibling:

$("div#selected").prevAll();

To access all previous sibling untill some point,The parenthesis takes tag name, class name, id etc.:

$("div#selected").prevUntill();

Similarly to get next siblings

$("div#selected").next();

$("div#selected").nextAll();

$("div#selected").nextUntill();

Post a Comment for "Select Sibling Before An Element In Jquery?"