Skip to content Skip to sidebar Skip to footer

Javascript Appending Another Div Data On Div Tag Dynamically

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 question, ho

Solution 1:

Try this

var i = document.getElementById( 'old' );
var d = document.getElementById( 'new' );
d.innerHTML += i.innerHTML;

Solution 2:

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

<html><head><scripttype="text/javascript">functionadd() {

        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);

    }

    functionremoveLocation() {

        var d = document.getElementById( 'new1' );

        var p = document.getElementById('new');

        p.removeChild(d);

    }
        </script></head><body><divid="old">
            Content of old div
        </div><hr/><divid="new"></div><hr/><buttononclick="add();">Add</button><br><buttononclick="removeLocation();">Remove</button></body></html>

Post a Comment for "Javascript Appending Another Div Data On Div Tag Dynamically"