4

I'm trying to add a "disabled" class to an element which has a class name not in array, and I have an array output like this :

["xs", "xl", "s", "m"]

and the html markup like this:

<a href="js:;" class="pa_size xs" title="xs" data-select="pa_size" data-value="xs">XS</a>
<a href="js:;" class="pa_size xl" title="xl" data-select="pa_size" data-value="xl">XL</a>
<a href="js:;" class="pa_size s" title="s" data-select="pa_size" data-value="s">S</a>
<a href="js:;" class="pa_size m" title="m" data-select="pa_size" data-value="m">M</a>
<a href="js:;" class="pa_size l" title="l" data-select="pa_size" data-value="l">L</a>

I have tried something like this:

if ( $.inArray(val, array) !== -1 ) { ... }

or

$.each(array, function() { ... }

but no luck.

3
  • please show more context for where you are trying to use code shown. We can't help you resolve why without seeing how you are applying it Commented Jun 21, 2016 at 5:24
  • @mplungjan not complaining but using if ( $.inArray(val, array) !== -1 ) {...} should work in proper context is why i ask Commented Jun 21, 2016 at 5:28
  • Ah, I see. Yes we do not know how he got val Commented Jun 21, 2016 at 5:29

1 Answer 1

3

Generate multiple class selector using the array and avoid them using :not() pseudo-class or not() method in jQuery.

var classes = ["xs", "xl", "s", "m"];

$('a').not(classes.map(function(v) {
  // iterate and add `.` before each element
  return '.' + v; 
  // join using `,` to act them as multi selector
}).join(',')).addClass('class')
.class {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="js:;" class="pa_size xs" title="xs" data-select="pa_size" data-value="xs">XS</a>
<a href="js:;" class="pa_size xl" title="xl" data-select="pa_size" data-value="xl">XL</a>
<a href="js:;" class="pa_size s" title="s" data-select="pa_size" data-value="s">S</a>
<a href="js:;" class="pa_size m" title="m" data-select="pa_size" data-value="m">M</a>
<a href="js:;" class="pa_size l" title="l" data-select="pa_size" data-value="l">L</a>


Or even more simpler as @charlietfl suggested

var classes = ["xs", "xl", "s", "m"];

$('a').not('.' + classes.join(',.')).addClass('class')
.class {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="js:;" class="pa_size xs" title="xs" data-select="pa_size" data-value="xs">XS</a>
<a href="js:;" class="pa_size xl" title="xl" data-select="pa_size" data-value="xl">XL</a>
<a href="js:;" class="pa_size s" title="s" data-select="pa_size" data-value="s">S</a>
<a href="js:;" class="pa_size m" title="m" data-select="pa_size" data-value="m">M</a>
<a href="js:;" class="pa_size l" title="l" data-select="pa_size" data-value="l">L</a>

Sign up to request clarification or add additional context in comments.

6 Comments

Even simpler $('a').not('.' + classes.join(',.'))
@charlietfl - even better for this example. I do like the map too though
@TommyPradana : use removeClass('disabled')
@TommyPradana : if you like to combine with them then use $('a').not('.' + classes.join(',.') + '.disabled').addClass('class')
Thanks for all, :)
|

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.