www.webdeveloper.com
Results 1 to 4 of 4

Thread: removeChild Node

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    38

    removeChild Node

    I created 2 div-element dynamically with javaScript. How do I remove both div-elements with a button inside the second one?

    This onClick event removes only the second div:

    var div1= '<div id="a1">
    </div>';

    var div2= '<div id="a2">
    <button onclick="this.parentNode.parentNode.removeChil(this.parentNode);>
    </div>';

    Extra:
    Is there a way to remove all child nodes?
    Last edited by massalexx; 10-07-2009 at 02:05 PM.

  2. #2
    Join Date
    Aug 2009
    Posts
    38
    Is there a way to remove a thread in this site? I can't find the remove button.
    Last edited by massalexx; 10-07-2009 at 02:07 PM.

  3. #3
    Join Date
    Aug 2009
    Posts
    38
    I found a half-solution that removes both elements first time but removes only second element afterward. I put a form-tag around both div and remove all childs of this form.

    <script type='text/javascript'>
    function removeChildrenFromNode()
    {
    var cell = document.getElementById("divForm1");
    if (cell.hasChildNodes()){
    while (cell.childNodes.length >= 1)
    cell.removeChild(cell.firstChild);
    }
    var cell = document.getElementById("divForm2");
    if (cell.hasChildNodes()){
    while (cell.childNodes.length >= 1)
    cell.removeChild(cell.firstChild);
    }
    }
    </script>

    var div1= '<form id="divForm1"><div id="a1"></div></form>';
    var div2= '<form id="divForm2"><div id="a2"><button onclick="removeChildrenFromNode();"></div></form>';

  4. #4
    Join Date
    Aug 2009
    Posts
    38
    does someone know how to modify this to a full-solution? (so it ALWAYS remove both elements).(i need it because i generate these both div dynamically with a button inside document and i need to remove these 2 div afterwards)
    thx,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles