By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,231 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

Remove a Node/element

P: n/a
Is there any other way to remove a node/element other than going into the
Parent and do "removeChild" ?

thanks
(need to remove a div of id "foobar")
Sep 10 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Boobie wrote:
Is there any other way to remove a node/element other than going into the
Parent and do "removeChild" ?


There are many ways to skin a cat, but likely the simplest way to remove
a node is to use removeChild. If you know the id of the node you want
to remove and have performed suitable feature detection, you can use:

var el = document.getElementById( someId );
el.parentNode.removeChild( el );

Other methods are possible, but they are almost certainly longer and
less efficient. If you explain why you want to use some other method,
perhaps more specific advice can be given.

--
Rob
Sep 11 '05 #2

P: n/a

"Boobie" <bo********@mailnull.com> wrote in message news:df**********@reader1.panix.com...
Is there any other way to remove a node/element other than going into the
Parent and do "removeChild" ?

thanks
(need to remove a div of id "foobar")


no, but it isn't that complicated :)

in your case:

var NParent, NNested, NThrowaway;
NNested = document.getElementById("foobar");
NParent = NNested.parentNode;
NThrowaway = NParent.removeChild(NNested);

or just

document.getElementById("foobar").parentNode.remov eChild(document.getElementById("foobar"));

now, if your <div id="foobar"> isn't a childNode of any other element, then you're stumped.
You could append the node to the body and then remove it...

well, the NETframework apparently does have a direct way:
http://msdn.microsoft.com/library/en...asp?frame=true
If you are working with an XmlNamedNodeMap, you can remove a node using the
RemoveNamedItem Method.
http://msdn.microsoft.com/library/en...dItemTopic.asp
Sep 11 '05 #3

P: n/a
Robi wrote:
"Boobie" <bo********@mailnull.com> wrote in message news:df**********@reader1.panix.com...
Is there any other way to remove a node/element other than going into the
Parent and do "removeChild" ?

thanks
(need to remove a div of id "foobar")

no, but it isn't that complicated :)

in your case:

var NParent, NNested, NThrowaway;
NNested = document.getElementById("foobar");
NParent = NNested.parentNode;
NThrowaway = NParent.removeChild(NNested);

or just

document.getElementById("foobar").parentNode.remov eChild(document.getElementById("foobar"));

now, if your <div id="foobar"> isn't a childNode of any other element, then you're stumped.


If the div is part of the document, its parentNode will be either the
body or a child of the body. A document must have a body - even if
there is no body tag in the source HTML the browser will make one.

"Interface HTMLBodyElement

"The HTML document body. This element is always present in the DOM
API, even if the tags are not present in the source document."

<URL:http://www.w3.org/TR/2002/CR-DOM-Level-2-HTML-20021007/html.html#ID-62018039>

If the div has no parent, then it isn't part of the document and its
'removal' is dependent on how it was created - it may not be
explicitly required at all.

[...]
--
Rob
Sep 11 '05 #4

P: n/a
thanks all for your input

I was really scratching my head why the hell the spec doesn't allow
something like "removeByID"
Sep 11 '05 #5

P: n/a
Boobie wrote:
thanks all for your input

I was really scratching my head why the hell the spec doesn't allow
something like "removeByID"

Create your own function (using RobG's logic):

function removeByID(id){
var el;
if((el=document.getElementById(id)) && el.parentNode){
el.parentNode.removeChild(el);
}
}

Mick
Sep 11 '05 #6

P: n/a
RobG wrote in message news:43***********************@per-qv1-newsreader-01.iinet.net.au...
Robi wrote:
"Boobie" <bo********@mailnull.com> wrote in message news:df**********@reader1.panix.com...
Is there any other way to remove a node/element other than going into the
Parent and do "removeChild" ?

thanks
(need to remove a div of id "foobar")


no, but it isn't that complicated :) [...] document.getElementById("foobar").parentNode.remov eChild(document.getElementById("foobar"));

now, if your <div id="foobar"> isn't a childNode of any other element, then you're stumped.


If the div is part of the document, its parentNode will be either the
body or a child of the body. A document must have a body - even if
there is no body tag in the source HTML the browser will make one.

"Interface HTMLBodyElement

"The HTML document body. This element is always present in the DOM
API, even if the tags are not present in the source document."

<URL:http://www.w3.org/TR/2002/CR-DOM-Level-2-HTML-20021007/html.html#ID-62018039>

If the div has no parent, then it isn't part of the document and its
'removal' is dependent on how it was created - it may not be
explicitly required at all.


well, since I don't know how the the "foobar" <div> was created.
lets say it was created like this:
NodeId = document.createElement("div");
NodeId.id = "foobar"
NodeFound = document.getElementById("foobar") // won't find it.

this way the node exists, but isn't attached to neither the body
or any other element and thus not removable.
that's what I meant with
"You could append the node to the body and then remove it..."

I'm not sure how memory is used with "created and forgotten objects",
if you could actually create a memory leak that way...
Sep 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.