1

I have a function where I am iterating over each .marker, creating a variable that contains its classes.

I also have an array named checkBoxClasses.

What I'm having a problem with is checking the classes within the variable markerClasses against the array checkBoxClasses. I want to break down the variable markerClasses and pass each individual class through the array.

Here's the code so far:

$('.marker').each(function () {

      var markerClasses = $(this).attr('class').split(' ');

            if ($.inArray(markerClasses , checkBoxClasses) > -1) {

                //do something

            };
});
6
  • "I am aware that you cant pass a variable through this so Im asking for a way to do this." to do something with what ? Commented Jan 4, 2012 at 11:58
  • pass seperate class values through the inArray and if they match the checBoxClasses array, then //do something. Commented Jan 4, 2012 at 11:59
  • loop on the array markerClass then use inarray Commented Jan 4, 2012 at 12:00
  • do you want to 'do something' when all the markerClasses match the checkBoxClasses or just 1 or more? Commented Jan 4, 2012 at 12:02
  • I want to break down the variable markerClasses and pass each individual class through the array. Thats the best way to do it I think. Commented Jan 4, 2012 at 12:04

1 Answer 1

4

inArray checks for a single value in an array. Since the value of the array reference markerClasses isn't in checkBoxClasses, it will always return -1.

It's unclear what you want to do. If you want to know if any of the markerClasses entries is in checkBoxClasses, you'll need to loop them and check them individually, breaking on the first match. If you want to check if they're all in checkBoxClasses, it's similar but you break on the first non-match.

E.g., to see if any of the element's classes is in checkBoxClasses:

var markerClasses = $(this).attr('class').split(' ');
var found = false;
$.each(markerClasses, function(index, value) {
    if ($.inArray(value, checkBoxClasses) !== -1) {
        found = true;
        return false;
    }
}
if (found) {
    // At least one of the element's classes was in `checkBoxClasses`
}

To see if all of the element's classes are in checkBoxClasses:

var markerClasses = $(this).attr('class').split(' ');
var allFound = true;
$.each(markerClasses, function(index, value) {
    if ($.inArray(value, checkBoxClasses) === -1) {
        allFound = false;
        return false;
    }
}
if (allFound) {
    // *All* of the element's classes was in `checkBoxClasses`
    // (Including the case where the element didn't have any.)
}
Sign up to request clarification or add additional context in comments.

2 Comments

edited the bottom line to make this more informative. I want to pass separate classes through this to see if they match.
@CecilTheodore: I'm afraid the question is still fairly unclear. Hopefully the answer addresses it anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.