Jump to content

Dom Elements


clonetrooper9494

Recommended Posts

I read a tutorial online about removing and appending elements, and I tried to use the example they have there. For some reason, the code they gave me Appends a div, but it doesn't Remove it... I looked up the function removeChild, and I can't find any problems with his code!

<script>function addElement() {  var ni = document.getElementById('myDiv');  var numi = document.getElementById('theValue');  var num = (document.getElementById('theValue').value -1)+ 2;  numi.value = num;  var newdiv = document.createElement('div');  var divIdName = 'my'+num+'Div';  newdiv.setAttribute('id',divIdName);  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'java script:;\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';  ni.appendChild(newdiv);}function removeElement(divNum) {  var d = document.getElementById('myDiv');  var olddiv = document.getElementById(divNum);  d.removeChild(olddiv);alert('No Errors Yet!');}</script><input type="hidden" value="0" id="theValue" /><p><a href="java script:;" onclick="addElement();">Add Some Elements</a></p><div id="myDiv"> </div>

FYI, its from http://www.dustindiaz.com/add-and-remove-h...ith-javascript/(I didn't use the Reprised version because I don't understand a single word of it)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...