Given a jQuery element, how can I determine if the sibling on the right is a text node and not another element? In PHP, you would compare the nodeType
to #text
- what's the equivalent in this case?
window.jQuery('body').find('a').each(function (i) {
if(window.jQuery(this).next() == '?'){
}
});
I am trying to work out what I can put in the condition part.
if(window.jQuery(this).next().length != 0){
alert(window.jQuery(this).next().get(0).nodeType);
if(window.jQuery(this).next().get(0).nodeType == 3){
alert('right has text');
}
For some reason, all my tests keep returning a 1 rather than a 3 to indicate text nodes!
next()
only returns elements, so you can't use it to traverse text nodes. You could instead use the DOM nextSibling
property and check its nodeType
property:
Live demo: http://jsfiddle.net/kD9qs/
Code:
window.jQuery('body').find('a').each(function (i) {
var nextNode = this.nextSibling;
if (nextNode && nextNode.nodeType == 3) {
alert("Next sibling is a text node with text " + nextNode.data);
}
});
as the comment:
check this nodeType and see if helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With