...

View Full Version : What while statement for this removeChild?



Opally
08-31-2007, 10:59 PM
Hi!

I'd like to execute this only once, and can't figure out the correct while syntax.

this while doesn't work, but the span removal does.


var statelabel = document.getElementById('statelabel');
var span = statelabel.getElementsByTagName('span');
while (statelabel.childNode(span[0])) {
statelabel.removeChild(span[0]);
}

I'm removing a <span></span> tag from inside a label element.

thanks!

---opally

Arbitrator
09-01-2007, 05:04 AM
I'd like to execute this only once, and can't figure out the correct while syntax.If you want to execute the code once, then why are you using a loop?


this while doesn't work, but the span removal does.If the span element is being removed, then the while loop must be working. I don’t see how that can be the case here though since there’s no method called “childNode” and, thus, an error should be thrown. (There is a property called childNodes, however.)



while (statelabel.childNode(span[0])) {
statelabel.removeChild(span[0]);
}The below code will work.


while (span[0]) {
statelabel.removeChild(span[0]);
}Of course, the while loop seems unnecessary. The below code omits it.


statelabel.removeChild(span[0]);

If you need to test for existence, than you can use an if statement.


if (span[0]) {
statelabel.removeChild(span[0]);
}


I'm removing a <span></span> tag from inside a label element.<span></span> is not a tag; it’s two tags. You don’t remove tags via removeChild; you remove elements.

Opally
09-01-2007, 02:37 PM
thank you, Arbitrator!

Works perfectly, thanks for taking a few moments to reply and help me understand better how the code works!!!

---Opally



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum