views:

55

answers:

2

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?

+3  A: 

Use previousSibling and nextSibling I would say.

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

wombleton
newline isn't falsy.
Luca Matteis
You could do `textContent.replace(/\s+/g, '')` on the text node contents before testing them.
Pointy
A: 

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.

Pointy