I can think of two approaches off-hand:

1. Use Enumerable#include on the array of classes you want to test
against, using Element#hasClassName as the iterator function (bound to
the instance), e.g.:

    var test = ['foo', 'bar', 'foob'];
    if (test.any(element.hasClassName.bind(element))) {
        // ...


    if (['foo', 'bar', 'foob'].any(element.hasClassName.bind
(element))) {
        // ...

(You don't need to use $A() around array literals.)

2. Use $w on the class name to get an array of the individual class
names, then use Enumerable#any on that, passing in Enumerable#include
on the array you want to test against as the #any iterator.  Something
like this:

    var test = ['foo', 'bar', 'foob'];
    if ($w(element.className).any(test.include.bind(test))) {

(Both of those bits of code are untested.)

But if this is going to be happening a lot (a tight loop, or a
mouseover handler, etc.), check the runtime cost of each of these by
going under the covers.

T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On May 13, 7:34 pm, louis w <louiswa...@gmail.com> wrote:
> I have an array containing a number of strings. I would like to
> continue exucuting my script only if an item has ANY/ALL of the
> strings assigned as a class name.
> $A(['foo', 'bar', 'foob']);
> Is there an elegant want to do this without having to loop through the
> items?
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to