[whatwg] getElementsByIdPart

chriswa at comcast.net chriswa at comcast.net
Thu Aug 26 11:33:16 PDT 2004

Thanks Ian for your response.

What I'm looking for is a way to apply a regular expression that can return matched elements.  What I am doing is taking advantage of the way ASP.NET generates id names and use javascript to group them together as structures.

For example:

a - <input id="outer1_inner1_element">
b - <input id="outer1_inner1_image">
c - <input id="outer1_inner2_element">
d - <input id="outer1_inner2_image">
e - <input id="outer2_inner1_image">
f - <input id="outer2_inner1_image">

What I'd like to do is the following:

var elOuter1 = getElementsById(/outer1/)   // returns elements a - d
var elOuter1_Inner1 = getElementsById(/outer1_inner1/)  //  returns elements a, b

The point is that rather than having an exact match as getElementsByName does now you can match on segments of the id attribute.  Currently the only way to do this now is to do the following:

var inputElements = getElementsByTagName( "input" );
var elOuter1 = [];
for ( var ii = 0; ii < inputElements.length; ++ii )
    if ( inputElements[ii].indexOf( "outer1" )  != -1 )
         elOuter[ii] = inputElments[ii];

I am assuming that having an instrinic function (most likely written in C++) would be more performant than doing the above JavaScript and since the information is already available to the DOM and I'm assuming that this would be a trival function to implement.  I appreciate the suggessions to use XPATH but I haven't found an easy, straightforward and compatible solution (as HTML docs are incompatable).



> On Sat, 21 Aug 2004, Doron Rosenberg wrote:
> >
> > Wouldn't a getElementsByAttribute be a more flexiable solution?
> It depends, what exactly did you have in mind?
> Note that the "." selector in CSS can't be directly emulated even with a 
> single "[ =~ ]" selector.
> I think for most uses, DOM Traversal is the way to go here. 
> getElementsByClass is IMHO an exception, in that implementing it as a node 
> filter is non-trivial.
> -- 
> Ian Hickson               U+1047E                )\._.,--....,'``.    fL
> http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
> Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list