ShadowIce
05-17-2009, 11:57 PM
Hi all :) I have been doing research on parent object nodes & child nodes. I have a small problem with the code I made. What I need for it to do is depending on which link u click, it adds or deletes the node specified in the function. And the name of the div shouldnt matter as it should be used when clicking on the link <a href="javascript:addevent('divname');">Add Element</a>.
for instance:
"Add element" | "Delete element"
if u click on the add element, then it adds an element, and if u click on the delete element, then it deletes the added element.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add/Remove child: Javascript</title>
<style type="text/css" media="screen">
.Welcome {
background: #FFE2AF;
color: #000;
border: 2px solid #ccc;
padding: 1em;
}
</style>
</head>
<script>
var num = 0;
function addEvent(myDiv) {
var ni = document.getElementById(myDiv);
var numi = document.getElementById('theValue');
num++;
numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "Element Number " + num + " has been added! <a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove the element ""+divIdName+""</a>";
ni.appendChild(newdiv);
}
function removeElement(divNum) {
var d = document.getElementById(divNum);
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
num -= 1;
}
</script>
<body>
<div class="welcome">
<H1>Welcome!</H1>
</div>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onClick="addEvent('myDiv');">Add Some Elements</a></p>
<p><a href="javascript:;" onClick="addEvent('myDiv1');">Add Some Elements</a></p>
<div id="myDiv"></div>
<div id="myDiv1"></div>
</body>
</html>
ANY help is GREATLY appreciated! :)
Thanks! :)
~SI~
for instance:
"Add element" | "Delete element"
if u click on the add element, then it adds an element, and if u click on the delete element, then it deletes the added element.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add/Remove child: Javascript</title>
<style type="text/css" media="screen">
.Welcome {
background: #FFE2AF;
color: #000;
border: 2px solid #ccc;
padding: 1em;
}
</style>
</head>
<script>
var num = 0;
function addEvent(myDiv) {
var ni = document.getElementById(myDiv);
var numi = document.getElementById('theValue');
num++;
numi.value = num;
var divIdName = "my"+num+"Div";
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);
newdiv.innerHTML = "Element Number " + num + " has been added! <a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove the element ""+divIdName+""</a>";
ni.appendChild(newdiv);
}
function removeElement(divNum) {
var d = document.getElementById(divNum);
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
num -= 1;
}
</script>
<body>
<div class="welcome">
<H1>Welcome!</H1>
</div>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onClick="addEvent('myDiv');">Add Some Elements</a></p>
<p><a href="javascript:;" onClick="addEvent('myDiv1');">Add Some Elements</a></p>
<div id="myDiv"></div>
<div id="myDiv1"></div>
</body>
</html>
ANY help is GREATLY appreciated! :)
Thanks! :)
~SI~