Evan
Evan

Reputation: 3501

Using an Array to check for this class

I have this idea where I want to check if the class exists when the button is clicked, but using an array that way I don't have to write it like this:

$(".example1,.example2,.example3,.example4,.example5").click(function() {
                myClass = $(this).attr("class").split(' ')[2];
                BtnRank = $(this).index('button');
                alert('BtnRank: '+BtnRank+' '+myClass);
                return false;
});

Instead it would look like this:

var searches = ['.example1','.example2','.example3','.example4','.example5'];

What's the best approach to make this work properly?

Upvotes: 0

Views: 26

Answers (1)

tymeJV
tymeJV

Reputation: 104775

You can do:

var selector = searches.join(", "); //Create concatenated selector

$(selector).click(function() {} )

Upvotes: 1

Related Questions