Forum Moderators: open
Wrote a function to return a real nextSibling, thought I'd post it here in case others have similar headaches.
function getNextSibling(el) {
el = el.nextSibling;
while (el.nodeType!=1) {
el = el.nextSibling;
}
return el;
}
Usage: (returns [object UL])
<p onclick="alert(getNextSibling(this))">Testing</p>
<ul>
<li></li>
</ul>
I think you might be reaching for the paracetamols with that one too, I'm afraid.
It doesn't check to see if there is a nextSibling, so it errors if you use the last element as input.
Try this one:
<script>
function getNextSibling(el)
{
while((el=el.nextSibling) && el.nodeType!=1);
return el;
}function test(el)
{
var next = getNextSibling(el);
if(next) next.style.backgroundColor = 'blue';
else alert('no nextSibling');
}</script>
<ul>
<li onclick="test(this);">some text</li>
<li onclick="test(this);">some text</li>
<li onclick="test(this);">some text</li>
<li onclick="test(this);">some text</li>
</ul>