JavaScript Appending another Div data on Div Tag Dynamically

前端 未结 2 1313
渐次进展
渐次进展 2021-02-01 11:47

I want to append the content of an already defined \"old div\" tag to the \"new div\" tag dynamically but its not working. The code i tried is attached below. And one more quest

相关标签:
2条回答
  • 2021-02-01 11:54

    ok I solved it. There was some error in my code. Its working now.

        <html>
        <head>
            <script type="text/javascript">
    
                function add() {
    
            var i = document.getElementById( 'old' );
    
            var d = document.createElement( 'div' );
            d.id = "new1";
            d.innerHTML = i.innerHTML ;
            var p = document.getElementById('new');
    
            p.appendChild(d);
    
        }
    
        function removeLocation() {
    
            var d = document.getElementById( 'new1' );
    
            var p = document.getElementById('new');
    
            p.removeChild(d);
    
        }
            </script>
    
        </head>
        <body>
            <div id="old">
                Content of old div
            </div>
    
            <hr/>
            <div id="new">
            </div>
            <hr/>
            <button onclick="add();">Add</button><br>
            <button onclick="removeLocation();">Remove</button>
        </body>
        </html>
    
    0 讨论(0)
  • 2021-02-01 11:59

    Try this

    var i = document.getElementById( 'old' );
    var d = document.getElementById( 'new' );
    d.innerHTML += i.innerHTML;
    
    0 讨论(0)
提交回复
热议问题