Page 1 of 1

removeChild() function (js)

Posted: Wed May 23, 2007 10:13 pm
by AluminX
i'm not sure how this function works in javascript. I'm trying to remove an element if it exists

Code: Select all

structure
<td id=mainFrame><p id="mytxt">this is my p element</p></td>

Code: Select all

if(document.getElementById('mytxt')){
			alert("Trace In");
			var removeThis = document.getElementById('mytxt');
			removeThis.removeChild();
		}
anyone can tell me whats wrong with it?
thanks

edit: umm removeChild removes a child of an elemet right? parentElem.removeChild('childToBeRemoved')?

also since i'm on the subject whats the difference between " " and ' ', when do i use one and when do i use the other?

Posted: Thu May 24, 2007 4:19 am
by Kieran Huggins
you need to call removeChild on the parent of the node you want removed:

Code: Select all

if(nodeToRemove = document.getElementById('mytxt')){
   nodeToRemove.parentNode.removeChild(nodeToRemove);
}
Single and double quotes are interchangeable in javascript, it's mostly a case of which one you'll have to escape less.

In PHP however, there are subtle differences. For instance "\n" is a newline, while '\n' is a backslash followed by the letter "n". Good times.