Removing a DOM node

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Removing a DOM node

Post 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]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

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