Link to home
Start Free TrialLog in
Avatar of wellso
wellsoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding & Removing Nodes in JavaScript

HI there,

I am using this code to generate <a> nodes that remove themselves when clicked. However I can't seemt o get the removeNode fucntion to work, Firebug shows the error I am reciving as

uncaught exception: Node was not found (NS_ERROR_DOM_NOT_FOUND_ERR)
[Break on this error] docBody.removeChild(node);

Can anyone point me in the right direction?
function addNode(id) {
	var inText = document.getElementById(id).innerHTML;
	var newText = document.createTextNode(inText);
 
	var newContact = document.createElement("a");
	newContact.setAttribute('id',id); 
 
	newContact.appendChild(newText);
	
	newContact.setAttribute('href','#'); 
	newContact.setAttribute('onclick','removeNode('+id+')'); 
	+
 
	var docBody = document.getElementById("contactlist");
	docBody.appendChild(newContact);
 
	document.getElementById('livesearch').style.display = 'none';
	document.getElementById('txt1').value = '';
 
	return false;
}
	
function removeNode(id)
{
	var docBody = document.getElementById("contactlist");
	var node = document.getElementById(id);
	docBody.removeChild(node);
	
}

Open in new window

Avatar of dan_neal
dan_neal
Flag of United States of America image

Try this:
function addNode(id) {
	var inText = document.getElementById(id).innerHTML;
	var newText = document.createTextNode(inText);
 
	var newContact = document.createElement("a");
	newContact.setAttribute('id',id); 
 
	newContact.appendChild(newText);
	
	newContact.setAttribute('href','#'); 
	newContact.setAttribute('onclick','removeNode('+id+')'); 
	+
 
	var docBody = document.getElementById("contactlist");
	docBody.appendChild(newContact);
 
	document.getElementById('livesearch').style.display = 'none';
	document.getElementById('txt1').value = '';
 
	return false;
}
	
function removeNode(id)
{
//	var docBody = document.getElementById("contactlist");
//	var node = document.getElementById(id);
//	docBody.removeChild(node);
         document.getElementById(id).removeNode(true);
	
}

Open in new window

Avatar of wellso

ASKER

FIrebug is returning

document.getElementById(id).removeNode is not a function

maybe because of a name conflict with what i have called the fucntion?
Avatar of HonorGod
It wouldn't be a function if the result of document.getElementById(id) is null (i.e., element is not found).
ASKER CERTIFIED SOLUTION
Avatar of dan_neal
dan_neal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wellso

ASKER

Thanks, that worked great, think Mozilla wasnt liking the removeNode method.