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/