Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the addChild method of javascript to add a node to my div?

Tags:

javascript

php

Hello I have a div as follows

<div id="test" class="test2">
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
<p> <a href=""> <a> </p>
</div>

(I have stripped out the data but that is the structure)

How does one use the addChild function of javascript to add a new node

<p> <a href=""> <a> </p>

I know how to use the removeChild function but I haven't been able to find documentation and examples using the addChild function.

If I have the node string in a php array, and can execute the php script containing the array with a GET call via a button click how can I use the addChild function to put the node string into the div?

like image 301
James Avatar asked May 26 '11 17:05

James


1 Answers

addChild is a php function, The javascript function is called appendChild:

var dv = document.createElement("p");
dv.innerHTML = "<a href=""> </a> ";
document.getElementById('test').appendChild(dv)

Alternativly, you can append the node string to the html: document.getElementById('test').innerHTML += '<p> <a href=""> <a> </p> '

like image 91
The Scrum Meister Avatar answered Oct 16 '22 12:10

The Scrum Meister