From: Matt Kruse on
Using a document listener on DOMNodeInserted, I'm trying to determine
if an element with id="x" was inserted. So either the
event.target.id=="x" or a child element of the one inserted
has .id=="x".

What is the best way to determine if #x is a child of the inserted
element?

Currently I'm doing:
if ( element.querySelectorAll("#x").length==1 )

But that limits my browser compatibility. I'd like to support Firefox
< 3.5, which lacks querySelectorAll.

Matt Kruse
From: Martin Honnen on
Matt Kruse wrote:
> Using a document listener on DOMNodeInserted, I'm trying to determine
> if an element with id="x" was inserted. So either the
> event.target.id=="x" or a child element of the one inserted
> has .id=="x".
>
> What is the best way to determine if #x is a child of the inserted
> element?
>
> Currently I'm doing:
> if ( element.querySelectorAll("#x").length==1 )
>
> But that limits my browser compatibility. I'd like to support Firefox
> < 3.5, which lacks querySelectorAll.

Well
element.getElementsByTagName('*')
gives you alld descendant elements, you could then iterate over that
node list and look for an element with the id you are interested in.
That should work back to Firefox 1.0 or IE 6 (which you are probably not
interested in anyway with using mutation events).
Firefox also supports XPath over HTML DOMs so you could also try to
solve that with XPath e.g.
element.ownerDocument.evaluate('descendant-or-self::*[@id = "' +
yourId + '"]', element, null, 9, null).singleNodeValue !== null
should tell you whether "element" itself or one of the descendant
elements has the id attribute.


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
From: Thomas 'PointedEars' Lahn on
Matt Kruse wrote:

> Using a document listener on DOMNodeInserted, I'm trying to determine
> if an element with id="x" was inserted. So either the
> event.target.id=="x" or a child element of the one inserted
> has .id=="x".
>
> What is the best way to determine if #x is a child of the inserted
> element?

Determine if #x's parent is the inserted element (`parentNode' property).


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Matt Kruse on
On May 27, 8:50 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> Matt Kruse wrote:
> > What is the best way to determine if #x is a child of the inserted
> > element?
> Determine if #x's parent is the inserted element (`parentNode' property).

I thought of this too, obviously, but it has the drawbacks of:
1. You may have to look through the parentNode chain all the way up to
the root document before realizing it's not a match, which seems
inefficient
2. If #x already existed in the doc somewhere else, you're going to be
inspecting this chain for no good reason

It seems that element.getElementById() would be handy in this case,
but perhaps performance using any of these methods would be fast
enough that trying to optimize is pointless.

Matt Kruse
From: Thomas 'PointedEars' Lahn on
Matt Kruse wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Matt Kruse wrote:
>> > What is the best way to determine if #x is a child of the inserted
>> > element?
>> Determine if #x's parent is the inserted element (`parentNode' property).
>
> I thought of this too, obviously, but it has the drawbacks of:
> 1. You may have to look through the parentNode chain all the way up to
> the root document

You mean root _element_. You may not have to, since not all element types
may be descendants of all others.

> before realizing it's not a match, which seems inefficient

A linked list lookup is certainly not more inefficient than a tree search.

> 2. If #x already existed in the doc somewhere else, you're going to be
> inspecting this chain for no good reason

If #x already existed in the document somewhere else, you have a design
problem to solve first.

> It seems that element.getElementById() would be handy in this case,

It is. BTDT.

> but perhaps performance using any of these methods would be fast
> enough that trying to optimize is pointless.

Perhaps not.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>