removeChild() function (js)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
AluminX
Forum Newbie
Posts: 19
Joined: Sat Jul 17, 2004 9:53 am

removeChild() function (js)

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
Post Reply