02.12.2004 - document.getElementsByWhatever
I was recently asked by a friend if there was such a thing as document.getElementsByClassName. I told them that, no, not inherently, but it shouldn't be a difficult thing to write. So I wrote it, and it worked well.
Then I googled for it and found that I had been beaten to it several times over and in several languages. Also revealed in the search was that Simon Willison had written a particulary interesting addition to the getElementsBy family of methods: document.getElementsBySelector.
These clever folks got me to thinking on other ways to extend these methods, and one that I hit on that I think is pretty good and maybe even useful is what I'm calling document.getElementsByWhatever
The method takes two arguments: variant "whatever" and boolean "includeTextNodes". It will return an object array of any element it finds that meets the "whatever" criteria.
For example, document.getElementsByWhatever("title",0) will return a collection of objects that have title attributes specified. document.getElementsByWhatever("look out behind you",1) will return a collection of objects that have that phrase as part of their text node collection.
If you should think of any "whatevers" that should be included in this method, feel free to let me know. I've tested this in MSIE 6, Opera 7, Mozilla 1.6, Firefox 0.8 and Safari 1.2, and all do well with it.
Hit the link above for a demo page, or use this one.
Posted by chirs on February 17, 2004 @ 11:00 pm
Comments have been closed for this post.