Luca Matteis
Luca Matteis

Reputation: 29267

jQuery .next() and .prev() not working as they should!

I want to get the .next() and .prev() sibling of an HTML element without excluding TextNodes.

I basically need to understand if an element is directly sorrounded by <br> HTML elements.

This would return true:

<br>
<div></div>
<br>

This would return false:

<br>
Some text
<div></div>
<br>

BUT this needs to also return true:

<br>

<div></div>
<br>

The third example basically uses an empty TextNode, or blankspaces, or newlines.

What's the best way to do this?

Upvotes: 1

Views: 575

Answers (2)

Pointy
Pointy

Reputation: 413737

The jQuery code for "next()", "prev()", "parents()", "nextAll", "prevAll", and most others of that sort explicitly skip nodes that aren't elements (type 1). That behavior isn't optional or alterable without changing the fundamentals of the library.

Upvotes: 0

wombleton
wombleton

Reputation: 8376

Use previousSibling and nextSibling I would say.

If it's of type TextNode and the value is falsy go another step.

Upvotes: 3

Related Questions