SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: DOM Problems...

  1. #1
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    DOM Problems...

    Hi all,

    I am trying to write some code that empties an element using DOM methods, but it doesnt seem to be working.

    Page: http://chris.unigliding.co.uk/weatherjack/index.php

    Code In Question:
    Code:
     //Empty the element
      alert(eleFeedbackMsg.childNodes.length + " children in this node");
      for (i = 0; i < eleFeedbackMsg.childNodes.length; i++) {
       alert("Removing Child " + i + " => " + eleFeedbackMsg.childNodes[i].nodeName);
       eleFeedbackMsg.removeChild(eleFeedbackMsg.childNodes[i]); 
      }//for
    This is the behaviour I get:

    Click the 'Place Votes!' button, you get the IMG and the text. Behaving well so far. Messages '1 Child' and 'Removing child 0: #text', as expected (it starts off with '&nbsp;' in there).

    Click it again, you get the message '2 children', and then 'removing child 0: IMG'. It then removes the image, and adds another image / text. You now have 2x text, and 1x image.

    Click it a third time, you get '3 children', and 'removing child 0: #text', then 'removing child 1: #text', which it does. Then puts in another image and another text, so you now have 2x IMG and 1x text.

    Every time after that, it removes an IMG and the text, then just replaces it.

    Can anyone tell me why it doesn't remove the last node, unless it's the only node in the first place? Also, the first time it seems to remove the only node (Correct), the 2nd time it ignores the last node (the text), and the 3rd time it seems to ignore the MIDDLE node, and remove the 2 text ones, which are the first and last one. You could actually say it ignores the 2nd element every time.

    Any help would be greatly appreciated, I am fairly new to DOM methods / scripting!

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, feel free to ignore this thread. No sooner had I posted about it than I realised my idiotic mistake. When i remove a child, it affects the numbering of the remaining children, the end effect being the 2nd one gets shifted into 1st position, and then ignored.

    Whoops!

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Congratulations on figuring it out yourself.

    You're not the first and won't be the last person to encounter that issue.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

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