clonetrooper9494 Posted March 21, 2009 Share Posted March 21, 2009 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 More sharing options...
jeffman Posted March 21, 2009 Share Posted March 21, 2009 Yup, the internal quotes got messed up. The broken part should read like this:onclick=\'removeElement("'+divIdName+'")\' Link to comment Share on other sites More sharing options...
clonetrooper9494 Posted March 21, 2009 Author Share Posted March 21, 2009 Sweet, it works now, thanks DD! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.