The Road to HTML 5: getElementsByClassName()

November 11th, 2008 by Mark Pilgrim, Google

Welcome back to my semi-regular column, "The Road to HTML 5," where I'll try to explain some of the new elements, attributes, and other features in the upcoming HTML 5 specification.

The feature of the day is getElementsByClassName(). Long desired by web developers and implemented in Javascript libraries like Prototype, this function does exactly what it says on the tin: it returns a list of elements in the DOM that define one or more classnames in the class attribute. getElementsByClassName() exists as a method of the document object (for searching the entire DOM), as well as on each HTMLElement object (for searching the children of an element).

The HTML 5 specification defines getElementsByClassName():

The getElementsByClassName(classNames) method takes a string that contains an unordered set of unique space-separated tokens representing classes. When called, the method must return a live NodeList object containing all the elements in the document, in tree order, that have all the classes specified in that argument, having obtained the classes by splitting a string on spaces. If there are no tokens specified in the argument, then the method must return an empty NodeList. If the document is in quirks mode, then the comparisons for the classes must be done in an ASCII case-insensitive manner, otherwise, the comparisons must be done in a case-sensitive manner.

A Brief History of getElementsByClassName()

Can We Use It?

Yes We Can! As you can tell from the timeline, getElementsByClassName() is supported natively in Firefox 3, Opera 9.5, Safari 3.1, and all versions of Google Chrome. It is not available in any version of Microsoft Internet Explorer. (IE 8 beta 2 is the latest version as of this writing.) To use it in browsers that do not support it natively, you will need a wrapper script. There are many such scripts; I myself am partial to Robert Nyman's Ultimate GetElementsByClassName. It uses the native getElementsByClassName() method in modern browsers that support it, then falls back to the little-known document.evaluate() method, which is supported by older versions of Firefox (since at least 1.5) and Opera (since at least 9.27). If all else fails, Robert's script falls back to recursively traversing the DOM and collecting elements that match the given classnames.

And in conclusion

getElementsByClassName() is well-supported across all modern browsers except IE, and a performance-optimized open source wrapper script can cover IE and older browsers.

7 Responses to “The Road to HTML 5: getElementsByClassName()”

  1. Fyrd Says:

    As more and more browsers start supporting querySelector and querySelectorAll (including IE8b2), does getElementsByClassName still serve much purpose?

  2. Rik Says:

    There’s a really nasty bug in the implementation of getElementsByClassName in WebKit. See https://bugs.webkit.org/show_bug.cgi?id=19974 for more details. There’s a workaround but it makes it more difficult to use it right now.

  3. Rob Russell Says:

    “I suggest that participants in this thread reacquaint themselves with the discussion in the previous one before having it again.” We need an acronym for that…

  4. dustin potter Says:

    whoa there. sorry if this comes across as “harsh” but i think i’m missing something fundamental (or maybe the WG is). i’m very confused on why an HTML group would be attempting to specify ECMAScript functions (assuming you’re even talking about ECMA/Javascript… the actual usage is never mentioned anywhere I could find).

    when and why did you decide to go this route (specifying DOM APIs and scripting language behavior) in the first place? does the “ML” part of “HTML” confuse you? ;) having a DOM “interface” as a property collection with accessor tags (private, readonly, etc), i can understand. but methods, i think, are out of your “jurisdiction”. also, the link to the specification just starts throwing around “document.getElementByClassName(’foo’)” as if you can call it from anywhere. “document.getElementByClassName()” then?? not likely.

    and “quirks mode” is in your specification? seriously? you’re making displaying things inconsistently a language feature??

    techinically IE doesn’t even support EMCA/JavaScript at all, it supports JScript, right? so are you now telling ECMA, JScript, VBScript, Python, BF, and any other client-side language I can get a browser to support that they need to implement certain functions to be HTML5-compliant? my scripting language might not even HAVE a DOM, my language might not even ALLOW “getElementsByClassName” as an identifier, or pass “parameters” and return values in the way ECMAscript does. i think this API stuff is a little outside your scope, honestly. and tends to “freeze out” scripting language developers who want to do things any other way than the ECMAScript way.

  5. Ian Hickson Says:

    Dustin: The WHATWG isn’t just “the HTML group” — we’re defining a number of things, including the HTML language, its APIs, and its syntaxes.

    Originally “HTML5″ was actually called “Web Applications 1.0″, and indeed “web-apps” is still in the document’s URL. We renamed it as part of the cooperation effort with the W3C.

    Regarding quirks mode, our goal is to write a (group of) specification(s) that defines everything you need to implement a Web browser, and quirks mode is part of that, whether we like it or not, so it is defined.

    Regarding JScript vs JavaScript vs ECMAScript: The names are really a red herring; JScript and JavaScript are ECMAScript in the same way that Mozilla’s HTML or CSS implementations and IE’s HTML or CSS implementations are HTML and CSS. That is, there are bugs and vendor-specific variances, but by and large they’re the same thing.

    Regarding ECMAScript vs other languages: The HTML5 spec is mostly language-agnostic, it just uses WebIDL to define the interfaces.

  6. BARTdG Says:

    It may be obvious, but non-native getElementsByClassName() does not return a live NodeList, but an array. In some cases this may lead to different results in different browsers.

  7. Dev Blog AF83 » Blog Archive » Veille technologique : Wikimapia, April, CC, Rails, Firebug, etc. Says:

    [...] http://blog.whatwg.org/the-road-to-html-5-getelementsbyclassname : getElementsByClassName est une nouveauté d’HTML5. [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

purchase tramadol Carisoprodol Prescription order ultram; "hoodia gordonii" Phentermine Prices "vicodin prescription" 50 tramadol Order Carisoprodol Online Tramadol com tramadol hcl 991. cheap ambien Order Valium diet pills adipex cheap ultram; Vicodin For Sale Legal vicodin norco hydrocodone 927. order phentermine Phentermine Pharmacy order carisoprodol

phentermine sales,

Valium No Prescription ultram 50 mg phentermine prescriptions Xanax Xr phentermine without prescription ultram online Hoodia Gordoni ultram online tramadol 50mg, Buy Vicodin Online oxycodone vs hydrocodone 37.5 phentermine Phentermine Com vicadin xanax without prescription Ultram Buy hoodia weight buy lorazepam online

Hydrocodone Vicodin

tramadol prescription ionamin online Vicoden "hydrocodone 10" buy cheap soma Ultram Online phentermine for sale buy tramadol online Hydrocodone Lortab xanex, vicodin 500 Hydrocodone Pain "fastin" Cheapest phentermine com fastin 820. Phentermine Adipex hydrocodone drugs; phentermine on line Tramadol Sale ionamin diet pill buy xanax Buy Carisoprodol Online alprazolam online? rx phentermine; Order Valium Online alprazolam prescription, ionamin prescription Buy Ultram Online phentermine sale Buy valium online without a prescription order valium 315. Lorazepam Prescription ionamin diet pill alprazolam discount; Acomplia Hydrocodone apap hydrocodone canada 723. www ultram com Vicodin Hydrocodone adipex online phentermine on line 37.5 Phentermine "generic adipex" "buy zolpidem" Com Fastin order vicodin online order tramadol? Carisoprodol Soma drug fastin order alprazolam Adipex Pharmacy adipex p Hydrocodone apap hydrocodone canada 723. Hydrocodone buy phentermine online xanax prescription Vicodin Com ionamin diet pill purchase phentermine online Buy Ambien Online adipex pills purchase tramadol Ultram 50 Mg adipex no prescription Adipex adipex 37.5 992. Cheap Ultram buy ambien online adipex pills Xenical Prices online xanax! get phentermine Tramadol Cod Buy ativan online buy cheap xanax 352. buy carisoprodol online Ic Hydrocodone Apap buy soma discount carisoprodol Get Tramadol generic tramadol; fastin prescription! Darvocet Vicodin phentermine price discount valium online Cheap Adipex carisoprodol 350 compare phentermine Soma Prescription com fastin! xenical tablets Cheapest Tramadol Hydrocodone acetaminophen hydrocodone apap 843. ultram com Phentermine Hcl vicodin hp! adipex 37.5 50 Tramadol diazepam prescription order ultram online; Online Hydrocodone lorazepam online discount valium Buy Ativan tramadol rx buy carisoprodol Hydrocodone Cheap hydrocodone "get tramadol" Buy Cheap Tramadol tramadol sale ultram 50 Order Ativan ultram price phentermine on line Online Xanax adipex pill carisoprodol 350mg Overnight Tramadol "vicodin generic" online pharmacy phentermine Online Vicodin hydrocodone medication valium online; Vicodin Lortab cheap soma phentermine on line Buy Ambien order valium online xenical prices Phentermine Prescriptions alprazolam buy cheap adipex! Xenical Price phentermine price adipex com Ativan vicodin tablets, generic tramadol; Meridia Phentermine cheap valium drugs vicodin Alprazolam 2mg ionamin ic hydrocodone apap Order Soma order valium online discount phentermine Buy Cheap Phentermine low cost phentermine adipex 37.5 mg Diazepam 10mg xanax pharmacy vicodin com Ionamin medication tramadol, generic ambien Phentermine Diet Pill xanax no prescription discount soma Soma Pharmacy ultram er! generic hydrocodone, Tramadol Hcl order ultram; overnight tramadol Xenical Pill vicodin for sale order xanax Alprazolam 0.5mg cheap soma Phentermine diet phentermine diet pill 186. Buy Lorazepam Online phentermine 90 Phentermine weight loss phentermine without a prescription 989. Buy Tramadol Online buy lorazepam online "generic adipex" Phentermine On Line ultram tramadol hcl? Buy Soma Online phentermine hcl tramadol hydrochloride Hydrocodone 7.5 xanax prescription online vicodin Adipex Com xanax drug buy valium no prescription Zolpidem ambien online hydrocodone pill Vicodin 500 tramadol sale "www adipex com"
Hydrocodone Apap
tramadol alprazolam online pharmacy; Ultram Prescription ativan lorazepam tramadol sale Diet Adipex phentermine on line vicodin Legal Vicodin compare phentermine adipex sale Hydrocodone Mg tramadol pharmacy

alprazolam generic

Ultram Er

diazepam 5mg ambien generic Hydrocodone Acetaminophen xanax drug Hydrocodone pain hydrocodone pill 870. Hoodia Weight tramadol hcl? order valium online Tramadol Pharmacy ultram com

Legal vicodin norco hydrocodone 927.

Phentermine Capsules "vicodin prescription" vicodin com Hydrocodone For Sale Tramadol com tramadol hcl 991. cheap ambien Phentermine Price diet pills adipex cheap ultram; Cheap Soma Legal vicodin norco hydrocodone 927. order phentermine Ultram order carisoprodol phentermine sales, Adipex Ionamin ultram 50 mg prescription phentermine Diazepam Pharmacy phentermine without prescription ultram online Hydrocodone 500 ultram online tramadol 50mg, Get Phentermine oxycodone vs hydrocodone adipex no prescription Tramadolultram vicadin xanax without prescription Cheap Tramadol hoodia weight buy lorazepam online Fastin Prescription tramadol prescription phentermine 90 Order Tramadol "hydrocodone 10" buy cheap soma Generic Hydrocodone phentermine for sale buy tramadol online Generic Valium xanex, vicodin 500 Diazepam 5mg "fastin" Cheapest phentermine com fastin 820. Buy Alprazolam Online hydrocodone drugs; phentermine on line Adipex Without Prescription ionamin diet pill buy xanax Phentermine Without A Prescription alprazolam online? purchase phentermine
Alprazolam Prescription
alprazolam prescription, hoodia diet pill

Buying Vicodin

Buy valium online without a prescription order valium 315. diet pills phentermine Vicodin Prescription alprazolam discount; Hydrocodone apap hydrocodone canada 723. Hydrocodone Medication www ultram com adipex online Ativan Prescription phentermine on line "generic adipex" Taking Phentermine diazepam 10mg online vicodin Xenical Sales order tramadol? drug fastin Buy Xanax Online order alprazolam adipex p Purchase Tramadol Hydrocodone apap hydrocodone canada 723. buy phentermine online Purchase Hydrocodone xanax prescription ionamin diet pill Phentermine Canada www adipex! adipex diet pill Phentermine 15 purchase tramadol adipex no prescription Adipex Drug Adipex adipex 37.5 992. buy ambien online Xanax Com adipex pills online xanax! Ionamine get phentermine Buy ativan online buy cheap xanax 352. Order Adipex buy carisoprodol online buy soma Hydrocodone Canada generic soma generic tramadol; Buy Zolpidem fastin prescription! phentermine price Ultram Tramadol discount valium online carisoprodol 350 Drugs Vicodin compare phentermine buy phentermine online No Prescription Phentermine xenical tablets Hydrocodone acetaminophen hydrocodone apap 843. Buy Carisoprodol ultram com vicodin hp! Www Adipex adipex com diazepam prescription Carisoprodol 350mg order ultram online; lorazepam online Alprazolam 1mg discount valium tramadol rx Ultram Pharmacy buy carisoprodol hydrocodone Online Pharmacy Phentermine "get tramadol" tramadol sale Tramadol Online ultram 50 ultram price Xanex phentermine on line adipex pill Generic Xanax carisoprodol 350mg vicodin for sale Valium online pharmacy phentermine hydrocodone medication Generic Soma valium online; cheap soma Prescription Adipex Online phentermine on line order valium online Vicodin Medication xenical prices alprazolam buy Hoodia Diet cheap adipex! phentermine price Buy Valium Online Without A Prescription adipex com "vicodin prescription" Hydrocodone Pill generic tramadol; cheap valium Vicodin Without Prescription drugs vicodin ionamin Ativan Com ic hydrocodone apap order valium online Xanax Without Prescription discount phentermine low cost phentermine Alprazolam adipex 37.5 mg xanax pharmacy Adipex No Prescription vicodin on line medication tramadol, Buy Ionamin Online generic ambien xanax no prescription Www Ultram Com discount soma ultram er! Buy Ativan Online generic hydrocodone, order ultram; Xanax Prescription overnight tramadol vicodin for sale Alprazolam Xr order xanax cheap soma Generic Tramadol Phentermine diet phentermine diet pill 186. phentermine 90 Xanax 1mg Phentermine weight loss phentermine without a prescription 989. buy lorazepam online Www Alprazolam "generic adipex" ultram 2mg Xanax tramadol hcl? phentermine hcl Order Ultram tramadol hydrochloride xanax prescription Phentermine 30mg online vicodin xanax drug Hoodia Gordonii buy valium no prescription ambien online Alprazolam Discount hydrocodone pill tramadol online Buy Cheap Xanax "www adipex com" overnight tramadol Buy Hydrocodone Buy ativan online buy cheap xanax 352.

"));