remove child node

When I execute this script I get "No such interface supported".  It's supposed to remove the <span>  What am I doing wrong?
<!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 >
<script type="text/javascript">
	window.onload = function() {
	var elem =  document.getElementById("TD1");
	var spanElem=elem.getElementsByTagName("span");
	elem.removeChild(spanElem);
}
</script>
</head>
<body>
<Table>
<tr><td ID="TD1"><span>This is the span</span>Other text</td></tr></table>
</body>
</html>

Open in new window

PhotoCompManagerAsked:
Who is Participating?
 
HonorGodSoftware EngineerCommented:
for example
<html>
<body>
 
<table border='1'>
 <tr>
   <td ID="TD1">
     <span>This is the span</span>Other text
   </td>
 </tr>
</table>
 
<script type="text/javascript">
 
var td = document.getElementById( 'TD1' )
if ( td ) {
  var spans = td.getElementsByTagName( 'SPAN' )
  alert( '# spans: ' + spans.length )
  for ( var i = 0; i < spans.length; i++ ) {
    var span   = spans[ i ]
    var parent = span.parentNode
    parent.removeChild( span )
  }
} else {
  alert( 'Specified element not found: id="TD1".' )
}
 
 
</script>
 
</body>
</html>

Open in new window

0
 
mudbuggleCommented:
try
elem.removeChild(elem.firstChild);
0
 
PhotoCompManagerAuthor Commented:
That works, but don't know the child position in the real app. I suppose I have to loop through the children looking for the span
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
HonorGodSoftware EngineerCommented:
Or, given some parent node, you can use:

parentNode.getElementsByTagName( 'SPAN' )

which will return a (possibly empty) collection of all SPAN elements that are beneath the specified parentNode element...
0
 
PhotoCompManagerAuthor Commented:
Many thanks
0
 
HonorGodSoftware EngineerCommented:
You are welcome.  Thanks for the grade & points.

Good luck & have a great day.
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.

All Courses

From novice to tech pro — start learning today.