Skip to content Skip to sidebar Skip to footer

Why Won't This Script Append A Child Div Element?

When I click on the p element with an onclick attribute calling the make_child function I would expect it to append a div element when ever it is clicked but it seams to be only ap

Solution 1:

change the last line to this

target.appendChild(add);

now you are appending to the correct element

Solution 2:

Try doing target.appendChild(add) instead of target.appendChild(addtext)

Edit (more detail): The syntax for appendChild (from MDN) is:

var child = element.appendChild(child);

Where child is the element being appended. In this case, addtext = add.appendChild(text) is set to text rather than add. Just doing target.appendChild(add) should solve this problem.

This also means the variable addtext is useless; you can remove it leaving only

add.appendChild(text)

for that line.

Post a Comment for "Why Won't This Script Append A Child Div Element?"