www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem with DOM Scripting

  1. #1
    Join Date
    Jun 2007
    Posts
    24

    Problem with DOM Scripting

    Guys, I'm having issue with DOM scripting and need some help. Here's what i have:

    Code:
    ...
    
    <body>
       <div id="part1">
          ...
       </div>
    
       <!--  
          I want to insert another div tag here, right underneath the above div tag
          using DOM scripting.  What is the way ?  Thanks.
       -->
    </body>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You are looking, probably, for a sort of insertAfter() method. Here's the workaround:
    Code:
    function insertAfter(refNode,newNode){
    refNode.parentNode.insertBefore(newNode,refNode.nextSibling);
    }
    Example:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function insertAfter(refNode,newNode){
    refNode.parentNode.insertBefore(newNode,refNode.nextSibling);
    }
    onload=function(){
    var newNode=document.createElement('div');
    newNode.appendChild(document.createTextNode('text part 2'));
    var refNode=document.getElementById('part1');
    insertAfter(refNode,newNode);
    }
    </script>
    </head>
    <body>
    <div id="part1">text part 1</div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

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

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