- From: Dylan Schiemann <dylans@yahoo.com>
- Date: Fri, 27 Sep 2002 12:06:00 -0700 (PDT)
- To: "'www-dom@w3.org'" <www-dom@w3.org>
--- Robert Cavicchio <rcavicchio@akonix.com> wrote:
>
> I'm new to this list, so I apologize if this has
> been discussed before. I
> did a search of the archives and did not find
> anything about it.
>
> It appears that there are currently two generic
> methods for retrieving
> elements according to their identifiers,
> "getElementById" and
> "getElementsByTagName". One addition that would be
> very useful in HTML
> files is "getElementsByClassName".
>
> I sometimes use classes to filter elements for
> processing by a script, and
> the current methods for finding such elements are
> inefficient. If elements
> with different tag names might have a particular
> class, one must enumerate
> every element and check the "className" on each.
> This is further encumbered
> by the fact that any particular element might have
> multiple classes, so the
> value obtained by "className" must be parsed to see
> if it contains the
> specific class being searched for. A
> "getElementsByClassName" method would
> make such script code cleaner, and user agents could
> hopefully return this
> information much faster.
>
> ********************
> Rob Cavicchio
> Akonix Systems, Inc.
> rcavicchio@akonix.com
>
This was previously suggested and rejected in DOM
Level 3. I think it is a good idea, but it also makes
sense to not have it since it is very easy to
accomplish with xpath.
At http://groups.yahoo.com/group/wdf-dom/message/2308
, Jiri Znamenacek shows how to accomplish this with
xpath or current DOM implementations without too much
effort:
xpath:
//*[contains(@class, '_class_name_')]
dom:
var all = document.getElementsByTagName('*');
var allClass = new Array();
var re = /_class_name_/;
for (var i=0; i<all.length; i++)
if (all[i].getAttribute('_class_name_').search(re) !=
-1)
allClass[allClass.length] = all[i];
-Dylan Schiemann
http://www.sitepen.com/
http://www.dylanschiemann.com/
__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com
Received on Saturday, 28 September 2002 14:07:13 UTC