Daniel Eben Elmore wrote:
Is there a function like getElementsByClassName where I can use regex to
match the class names? I want to match class names starting with ...

You should be able to replace the getElementsByClassName function in
Prototype with the following (barely tested):

document.getElementsByClassName = function (pattern, parentEl ){
 if (typeof pattern == 'string') {
   pattern = new RegExp('(^|\\s)' + pattern + '(\\s|$)');
 }
 var el, els = ($(parentEl)||document.body).getElementsByTagName('*');
 var elements = [];
 var i = els.length;

 while (i--){
   el = els[i]
   if (el.className && pattern.test(el.className)){
     elements.push(Element.extend(el));
   }
 }
 return elements;
}

To get all the elements with a className that starts with 'foo' use:

 document.getElementsByClassName( /(^|\s)foo/ );

If you pass it a string, it will match the full className.  If you give
it a regular expression, it will use that.  It assumes that if pattern
isn't a string, it's a RegExp - you may want to test that first.

--
Rob


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to