The very nature of the text displayed reveals a certain - shall we say, 'resistance' to doing what you propose. However, it is fairly easy to achieve:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Add/Remove child: Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function addEvent() {
var ni = document.getElementById('myDiv');
addEvent.v++;
var divIdName = "my"+addEvent.v+"Div";
var newdiv = document.createElement('div');
newdiv.id = divIdName;
newdiv.className = 'updated';
newdiv.appendChild(document.createTextNode("Element Number " + addEvent.v + " has been added! "));
var newlink = document.createElement('a');
newlink.onclick = function(){removeElement(this);return false;};
newlink.href='javascript:remove();';
newlink.appendChild(document.createTextNode('Remove the element "'+divIdName+'"'));
newdiv.appendChild(newlink);
ni.appendChild(newdiv);
}
function removeElement(el) {
var d = document.getElementById('myDiv');
var olddiv = el.parentNode;
d.removeChild(olddiv);
for (var ds = d.getElementsByTagName('div'), i = ds.length-1; i > -1; --i){
ds[i].firstChild.nodeValue = ds[i].firstChild.nodeValue.replace(/\d+/, i+1);
ds[i].getElementsByTagName('a')[0].firstChild.nodeValue = ds[i].getElementsByTagName('a')[0].firstChild.nodeValue.replace(/\d+/, i+1);
}
addEvent.v = ds.length;
}
addEvent.v = 0;
</script>
<style type="text/css" media="screen">
.updated {
background: #FFE2AF;
color: #000;
border: 2px solid #ccc;
padding: 1em;
}
</style>
</head>
<body>
<p><a href="javascript:add();" onclick="addEvent();return false;">Add Some Elements</a></p>
<div id="myDiv"> </div>
</body>
</html>
Notes: Converted to valid HTML 4.01 strict. Dropped the use of 'innerHTML' in favor of pure DOM 2 methods. Eliminated the need for a hidden form element which in some browsers will 'remember' the number of created division after a refresh even though these created divisions will no longer be there.
Bookmarks