Page 1 of 1

Removing a DOM node

Posted: Wed Jan 03, 2007 5:44 am
by kettle_drum
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey folks,

Having an issue with trying to remove a node from the DOM inorder to be left with just plain text. The HTML is as follows:

[syntax="html"]
<li id="listitem"><a href="#abc" id="link"><input type="checkbox" id="cb" /> text here</a></li>
I can successfully extract the checkbox and the text, but then cant figure out how to isolate the checkbox as it doesnt seem to be a node.

Code: Select all

	nodeToRemove = document.getElementById("listitem");
	val = nodeToRemove.firstChild.innerHTML;
	window.alert(val);
Any ideas on how to just get the text value?

Thanks in advance.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jan 03, 2007 6:50 am
by Chris Corbyn
You want to remove the checkbox?

Code: Select all

var cb = document.getElementById("cb");
cb.parentNode.removeChild(cb);
Or you want to remove the <li /> ?

Code: Select all

var li = document.getElementById("listitem");
li.parentNode.removeChild(li);
Is the ID "listitem" unique to that page?

Posted: Wed Jan 03, 2007 8:42 pm
by kettle_drum
Yeah that was perfect thanks d11wtq. I was messing around with trying to remove it from the variable itself rather than just taking it straight out of the DOM.