views:

34

answers:

1

Using javascript, how do I get all DOM elements that start with a certain prefix like <prefix:suffix>

Code example:

<div>
    <foo:bar1>hello</foo:bar1>
</div>
<foo:bar2>world</foo:bar2>

But, without looping through ALL elements (for performance reasons)?

Thanks, Ran

+4  A: 

With getElementsByTagNameNS:

document.getElementsByTagNameNS("http://example.com/namespace/for/foo", "*");

(Assuming you are using XHTML with elements imported from another namespace and are actually serving it with the correct content-type).

David Dorward
Thanks David, but unfortunately the script is supposed to be called inside other's websites making it impossible to enforce the correct doctype and content-type implementation. :-/
ranbena
Throwing namespaces into arbitrary tag soup pages is a big no-no. Just don't go there. If you are providing HTML snippets, then provide HTML and not homebrew XML.
David Dorward