Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts

    What while statement for this removeChild?

    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.

    Code:
    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

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by Opally View Post
    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?

    Quote Originally Posted by Opally View Post
    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.)

    Quote Originally Posted by Opally View Post
    Code:
    while (statelabel.childNode(span[0])) {
    	statelabel.removeChild(span[0]);
    }
    The below code will work.

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

    Code:
    statelabel.removeChild(span[0]);
    If you need to test for existence, than you can use an if statement.

    Code:
    if (span[0]) {
    	statelabel.removeChild(span[0]);
    }
    Quote Originally Posted by Opally View Post
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    Opally (09-01-2007)

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thank you, Arbitrator!

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

    ---Opally


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •