View Single Post
Old 09-12-2012, 02:59 PM   PM User | #3
Raymond Cheyne
New Coder

 
Join Date: Sep 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Raymond Cheyne is an unknown quantity at this point
Quote:
Originally Posted by docco View Post
I can not understand what's wrong with the below. Can not remove child in Crome. Anyone help
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Add & Delete Child</title>
</head>
<body>
<script type=text/javascript>
var childID=0;
function addChild()
{
  var parent = document.getElementById('myDiv');
  var newChild = document.createElement('div');

  childID=childID+1;  
  var childName = 'Div'+childID; // set index for child for deleting later
  newChild.setAttribute('id',childName);
  newChild.innerHTML = 'Element '+childName+' has been added! <a href=\'#\' onclick=\'removeElement('+childName+')\'>Remove element "'+childName+'"</a>';
  parent.appendChild(newChild);
}
function removeElement(divName)
{
  var parent = document.getElementById('myDiv');
  var oldDiv = document.getElementById(divName);
  parent.removeChild(oldDiv);
}
</script>

<p><button onclick="addChild();">Add New Elements</button></p>
<div id="myDiv"> </div>

</body>

</html>
I suggestion putting your script in the <head>, not the <body>. Beyond that, sorry, I'm not sure.

Raymond
Raymond Cheyne is offline   Reply With Quote