Is there a function like getElementsByClassName where I can use regex to match the class names? I want to match class names starting with ... -Daniel --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Am afraid its quite late (early?) and I haven''t had time to check it but you could try something like this? function checkClassName(el, regEx) { el = (typeof el == "string") ? $(el) : el; return el.className.match(new RegExp(regEx)); } var elements = [ "myElement1", "myElement2", "myElement3", "myElement4" ]; for (var i = elements.length-1; i >= 0; --i){ if(checkClassName(elements[i], "##RegEx##")) { // Do stuff } else { // do other stuff } } On Jan 19, 2:50 am, Daniel Eben Elmore <danielelm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ... > > -Daniel--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ah, yes that code looks like it would check an element individually/explicitly. I think? But what about returning all elements in the document as an array like the getElementsByClassName function does? I suppose I could just modify the function, but didn''t know if there was a more elegant way. Thanks Daniel -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Aaron B Sent: Thursday, January 18, 2007 11:48 PM To: Ruby on Rails: Spinoffs Subject: [Rails-spinoffs] Re: getElementsByClassName starting with... Am afraid its quite late (early?) and I haven''t had time to check it but you could try something like this? function checkClassName(el, regEx) { el = (typeof el == "string") ? $(el) : el; return el.className.match(new RegExp(regEx)); } var elements = [ "myElement1", "myElement2", "myElement3", "myElement4" ]; for (var i = elements.length-1; i >= 0; --i){ if(checkClassName(elements[i], "##RegEx##")) { // Do stuff } else { // do other stuff } } On Jan 19, 2:50 am, Daniel Eben Elmore <danielelm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ... > > -Daniel--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Eben Elmore a écrit :> Ah, yes that code looks like it would check an element > individually/explicitly. I think? But what about returning all elements in > the document as an array like the getElementsByClassName function does? I > suppose I could just modify the function, but didn''t know if there was a > more elegant way.This could be stuff for a nice patch. I might look into it soon. In the meantime, can''t you reorganize your classes so that instead of using the structure of a single class name to categorize them, you treat class names as aspects, and equip the relevant element with several class names, some being used only for such retrieval? e.g. instead of: <elt1 class="toggledItem">...</elt1> <elt2 class="item">...</elt2> ... <elt3 class="toggledBar">...</elt3> -> document.getElementsByClassName(/^toggled/); // doesn''t work you''d go: <elt1 class="toggled item">...</elt1> <elt2 class="item">...</elt2> ... <elt3 class="toggled bar">...</elt3> -> document.getElementsByClassName(''toggled''); // works fine -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Daniel Eben Elmore wrote:> Is there a function like getElementsByClassName where I can use regex to > match the class names?There''s no such sugar currently implemented in Prototype, but, as Aaron showed you already, it''s quite easy to implement. Allegedly, a more efficient solution would be to use XPath, for which we have "document._getElementsByXPath" (example below). There is one "tiny" problem though (hence the underscore): this method is available only for browsers that support "document.evaluate" method. You''ll have to bake your own method for the "others" and submit a patch :) AFAIK Internet Explorer has an ActiveX object supporting XPath - Msxml.DOMDocument - but that''s all I heard ;-)> I want to match class names starting with ...document._getElementsByXPath(''.//*[contains(concat(" ", @class), " foo")]'') ^ this would return a list of elements that have class names starting with "foo". cheers - -- Marius Feraru -----BEGIN PGP SIGNATURE----- iD8DBQFFsJpdtZHp/AYZiNkRAlZ4AJ44xW5SR4vEDXqpesymPkHHfpR8AACeOYRC JpAltoOeWWbA6R6NGDsmUBU=Qk31 -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---