views:

70

answers:

1

Hello, I've a jQuery object and I shall retrieve the next sibling node, which may be a text node. For example:

<div id="test">some text<br/>other text</div>

var test = $("#test");
var br = $("br", test);
alert(br.next().length); // No next ELEMENTS
alert(br.get(0).nextSibling.nodeValue); // "other text"
alert(br.get(0).nextSibling.nextSibling); // null

The DOM level 2 allows to get the next NODE sibling, but jQuery next() works on elements (nodeType 1, I guess). I am asking this because I'm already using jQuery and I prefer to don't touch the DOM nodes directly (also because jQuery itself may provide a layer of abstraction from DOM and may run where DOM level 2 is not supported, but this is only a thought).

If jQuery doesn't provide this, shall I use DOM like above, or there are better options?

Thanks ~Aki

EDIT: I forgot something: I don't want to get ONLY text elements, but any kind of node, just as nextSibling does. I'm using .contents() to iterate over the content, but this is pretty annoying (and slow, and many other bad things) when you just need the next node and DOM provides a solution.

EDIT2: Looking jQuery source code, it seems it relies on nextSibling.

+2  A: 

Use the DOM. Don't be scared of it; it's easy and you already seem to know what to use. jQuery is built on top of the DOM and for this kind of thing, using the DOM will in fact work in more browsers than the jQuery version.

Tim Down
I just feel dirty when it comes to skip from an abstraction layer to another :D (as this usually nulls the meaning of abstractions)Thanks, I feel safe now. So, I guess it's safe to write my plugins using DOM manipulation...(?)EDIT: I feel much safer after looking at thishttp://www.quirksmode.org/dom/w3c_core.html#t74Thanks again :)
AkiRoss
Using the DOM directly is generally safe. There are some quirks around the edges in some browsers that jQuery does shield you from, particularly in IE, but they're reasonably well-known these days (hence easily searchable), and sites like QuirksMode are excellent resources. Just make sure you test frequently in IE :)
Tim Down