Jump to content

[SOLVED] Cant get removeChild working in IE/FF


arianhojat

Recommended Posts

I am trying to remove a child element from the document, but keep getting an error:

In IE, This error is: 'No Such interface supported'

In FF. The error is: 'uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMHTMLDivElement.removeChild]"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: file:///W:/ArianTest/HelpDesk/testRemoveChild.html# :: removeTheChild :: line 18"  data: no] Line 0'

 

 

Here is some basic example code, tried 2 ways to remove the hyperlink element.

EDIT: 2d way works, but id like to get 1st method working as i have usually used removeChild and wondering why its not working in this case.

 

<!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=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">

function removeTheChild()
{
var closeRequestDiv = document.getElementById('closeRequest');
var a = closeRequestDiv.getElementsByTagName('a');
var hyperlink1 = a[0];
alert('html='+ hyperlink1.innerHTML); //just to make sure it pulled a hyperlink node, so should output Close
//alert('a.#='+ a.length);

var aParent = hyperlink1.parentNode;
alert('test1');
aParent.removeChild(a);
alert('test2'); //never gets here
}

function deleteNode(elementId)
{
  var label=document.getElementById(elementId);	
  while( label.hasChildNodes() ) { label.removeChild( label.lastChild ); }
}

</script>
</head>
<body>

Both dont work:<br/>
<a href="#" onclick="removeTheChild()">Remove The Child</a>
<br/><br/>
<a href="#" onclick="deleteNode('closeRequest')">Remove The Child2</a>

<div class="request" id="requestID2793">
<div id="closeRequest">
<a href="#">Close</a>
</div>
</div>


</body>
</html>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.