Forum Moderators: open

Message Too Old, No Replies

removeChild method not working

         

mattr555

9:47 am on Feb 23, 2005 (gmt 0)

10+ Year Member



I have 2 functions, one to insert a div into the dom and one to remove a div. The remove function should remove the div where the div's ID number matches the number the user enters into the form text field. when i run this, IE reports a type mismatch.

Any ideas?

<html>
<head>
<title>DOM Scripting Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link id="stylelink" rel="stylesheet" type="text/css" href="jstest.css" media="screen" />

<script type="text/javascript">

var counter = 0;

function insertdiv(){

var newdiv = document.createElement('div');
var newpara = document.createElement('p');
var thetext = document.createTextNode("div"+counter);

newpara.appendChild(thetext);
newdiv.appendChild(newpara);
newdiv.setAttribute('class','floater')
newdiv.setAttribute('className','floater')
var idname = 'div'+counter;

newdiv.setAttribute('id',idname);
//newdiv.className=('floater')
document.getElementById('jstest').appendChild(newdiv);
counter++;

}

function deldiv(){
var formno = document.forms[0].elements[0].value;
var divno = 'div'+formno;

document.getElementById('jstest').removeChild(divno);
}

</script>

</head>
<body id="jstest">
<form id="frmDivNo">
<label for="txtDivNo">Enter Div Number</label>
<input id="txtDivNo" />
</form>
<a href="javascript:insertdiv()">click to insert div</a>
<a href="javascript:deldiv()">click to delete div</a>
</body>
</html>

SpaceFrog

9:58 am on Feb 23, 2005 (gmt 0)

10+ Year Member



what is formno intended to be?

SpaceFrog

10:04 am on Feb 23, 2005 (gmt 0)

10+ Year Member



<script type="text/javascript">

var counter = 0;

function insertdiv(){

var newdiv = document.createElement('div');
var newpara = document.createElement('p');
var thetext = document.createTextNode("div"+counter);

newpara.appendChild(thetext);
newdiv.appendChild(newpara);
newdiv.setAttribute('class','floater')
newdiv.setAttribute('className','floater')
var idname = 'div'+counter;
newdiv.setAttribute('id',idname);
document.getElementById('jstest').appendChild(newdiv);
counter++;
}

function deldiv(){
var formno = document.getElementsByTagName('div')[document.getElementsByTagName('div').length-1];
alert(formno.id)
document.getElementById('jstest').removeChild(formno);
}

</script>

mattr555

10:22 am on Feb 23, 2005 (gmt 0)

10+ Year Member



formno is the value the user types into the form. if the user type 3 then the function appends "div" to the 3 should remove a div with the id of "div3".

SpaceFrog

2:25 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



not with value ....

mattr555

10:44 am on Feb 24, 2005 (gmt 0)

10+ Year Member



worked it out:

function deldiv(){
var formno = document.forms[0].elements[0].value;
var divno = 'div'+formno;
if (!document.getElementById(divno))
{
alert('no div');
}
else
{
var oparent = document.getElementById('jstest');
var ochild = document.getElementById(divno);

oparent.removeChild(ochild);
}
}