W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2004

[whatwg] getElementsByIdPart

From: <chriswa@comcast.net>
Date: Thu, 26 Aug 2004 18:33:16 +0000
Message-ID: <082620041833.921.412E2CEB000CE5310000039922007614380E999C079D080C@comcast.net>
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).

Thanks,
Chris

 



> 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.   `._.-(,_..'--(,_..'`-.;.'
Received on Thursday, 26 August 2004 11:33:16 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:36 UTC