• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

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

0
wellso
Asked:
wellso
  • 2
  • 2
1 Solution
 
dan_nealCommented:
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

0
 
wellsoAuthor Commented:
FIrebug is returning

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

maybe because of a name conflict with what i have called the fucntion?
0
 
HonorGodSoftware EngineerCommented:
It wouldn't be a function if the result of document.getElementById(id) is null (i.e., element is not found).
0
 
dan_nealCommented:
AHHH!!! New idea.  The removeChild seems to be the right method but could be using the wrong means.  I know this seems to be what you were doing but it may be a matter of DOM handling.  Getting the .parentNode of the actual element you want to remove helps to ensure you have the right node.

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)
{
         //Get a reference to the parentNode of the clicked object.
	var docBody = document.getElementById(id).parentNode;
         //Get a reference to the object itself
	var node = document.getElementById(id);
         //Use the removeChild method on the parentNode object.
	docBody.removeChild(node);
	
}

Open in new window

0
 
wellsoAuthor Commented:
Thanks, that worked great, think Mozilla wasnt liking the removeNode method.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now