Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
72
rated 0 times [  74] [ 2]  / answers: 1 / hits: 45556  / 15 Years ago, fri, may 15, 2009, 12:00:00

I have an element grabbed from document.getElementById('the_id'). How can I get its next sibling and hide it? I tried this but it didn't work:



elem.nextSibling.style.display = 'none';


Firebug error was elem.nextSibling.style is undefined.


More From » element

 Answers
333

it's because Firefox considers the whitespace between element nodes to be text nodes (whereas IE does not) and therefore using .nextSibling on an element gets that text node in Firefox.



It's useful to have a function to use to get the next element node. Something like this



/* 
Credit to John Resig for this function
taken from Pro JavaScript techniques
*/
function next(elem) {
do {
elem = elem.nextSibling;
} while (elem && elem.nodeType !== 1);
return elem;
}


then you can do



var elem = document.getElementById('the_id');
var nextElem = next(elem);

if (nextElem)
nextElem.style.display = 'none';

[#99528] Monday, May 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devonw

Total Points: 311
Total Questions: 116
Total Answers: 111

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;