I use this code to add or remove certain nodes from a page I have on my site:
function nextChild() {
var elem = document.getElementById('formtbody');
var a = 0;
var b = elem.childNodes[a];
var node = b.style.display;
var lastnode = 0;
while (node !== "none") {
(lastnode++);
(a++);
}
(lastnode++);
var c = lastnode;
var therightnode = elem.childNodes[c];
return therightnode;
}
function addRemoveClass(option) {
var elem = document.getElementById('formtbody');
if (option === "add") {
nextChild().style.display = "";
} else if (option === "remove") {
elem.lastChild.style.display = "none";
elem.lastChild.form.reset();
}
}
I execute with addRemoveClass("add") and addRemoveClass('remove')
But when I try to add, it goes unresponsive. I think it’s getting stuck in an infinite loop but I can’t tell.
I’ve tried JS Lint but it didn’t find anything either.
What I’m trying to do with this script is find the first child node of formtbody with the style="display:none;" attribute and make it visible.
This code is an infinite loop if
node !== "none"when the loop starts:Nothing in the loop changes the value of
nodeso once the loop starts, it will never stop.Also, this syntax is odd and not required:
Remove the parens so it’s just:
Using the jQuery library (which makes cross browser DOM manipulation sooo much easier), here’s some code to find the first item in a list that is set to display: none.
Here’s a working version: http://jsfiddle.net/jfriend00/Um95a/
and the code:
HTML:
CSS:
Javascript (run after page is loaded):
Or, any even simpler version of the code using the :hidden selector:
http://jsfiddle.net/jfriend00/Xsgmu/