- From: <chriswa@comcast.net>
- Date: Thu, 26 Aug 2004 20:38:34 +0000
Ok, I finally worked up an example using XPATH that does what I want. document.getElementsById = function(term) { var xpathString = "//*[contains(@id,'" + term.toString() + "')]" var xpathResult = document.evaluate(xpathString, document, null, 0, null); var outArray = new Array(); while ((outArray[outArray.length] = xpathResult.iterateNext())) { } return outArray; } function OnLoad() { //alert( "loading" ); debugger; var elements = document.getElementsById( "FormItem1" ); alert( elements.length ); alert( elements.length ); for ( var ii = 0; ii < elements.length; ++ii ) { alert( ii + ": ==> " + elements[ii].id ); } } This works in Mozilla and I can use the indexOf technique for IE. Thanks to everyone who challenged my assumptions. Chris > 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 13:38:34 UTC