Hi there.
It's my firts post here in Your forum so I'd like to say HELLO WORLD
I admit that's my first steps in JS. It causes me a problem at a beginning
<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=\'#\' 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);
}
</script>
<html>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>
</html>
After pressing 'Add Some Elements' link a new element is shown in the screen.
For example element:
Element Number 3 has been added! Remove the div "my3Div"
... But when I press 'Remove the div "my3Div"' link - element is not deleted
Where can be the problem? I'll be thankful for tips. Greetings.
It's my firts post here in Your forum so I'd like to say HELLO WORLD
I admit that's my first steps in JS. It causes me a problem at a beginning
<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=\'#\' 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);
}
</script>
<html>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>
</html>
After pressing 'Add Some Elements' link a new element is shown in the screen.
For example element:
Element Number 3 has been added! Remove the div "my3Div"
... But when I press 'Remove the div "my3Div"' link - element is not deleted
Where can be the problem? I'll be thankful for tips. Greetings.