Is there a way of checking whether the object’s specific class is in array – I’ve done it with object’s ‘id’, but can’t do with ‘class’ – with ‘id’ it goes like this:
var arr = [ 'error', 'button', 'items', 'basket' ];
if (jQuery.inArray($(this).attr('id'), arr) == -1) {
// do something here
}
I would like to have something like the following, which doesn’t work:
var arr = [ 'error', 'button', 'items', 'basket' ];
if (jQuery.inArray($(this).attr('class'), arr) == -1) {
// do something here
}
Any idea?
Just to show you what I’m using it for:
var arr = [ 'error', 'button', 'items', 'basket' ];
$.each(data, function(k, v) {
if (jQuery.inArray($(this).attr('class'), arr) == -1 && $('.' + k).length > 0) {
$('.' + k).fadeOut(100, function() {
$(this).hide().html(v).fadeIn(100);
});
}
});
$(this).is(selector)checks if the given object or set of object matches the selector.[ 'error', 'button', 'items', 'basket' ].<class_name>we prepend the dot to each class name using maparray.map(function(cls) {return "." + cls;})array.join(',')The result is
$(this).is('.error, .button, .items, .basket')which matches if$(this)has any of the classes defined.