1

I want to highlight any element that has a class that starts with a_slot_ but doesn't end with pink or green in any of the class names. In this example the third element I feel like should be highlighted because it has class a_slot_orange and the other class doesn't contain pink or green.

Why isn't the third element highlighted (but the last is)? How do I highlight the third with the others?

$(document).ready(function() {
  $("[class^='a_slot_']").each(function(i,v){
     if(!$(this).attr('class').includes('green') && !$(this).attr('class').includes('pink')){
$(this).css('background-color', 'red');
     }
  });
  // also don't work....have the same result
  //$("[class^='a_slot_']:not([class$='green'],[class$='pink'])").css('background-color', 'red');
  //$("[class^='a_slot_']").not("[class$='green'],[class$='pink']").css('background-color', 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p class='a_slot a_slot_green'>Green</p>
<p class='a_slot a_slot_pink'>Pink</p>
<p class='a_slot a_slot_orange'>Orange (shouldn't I be highlighed too?)</p>
<p class='a_slot_green'>Green</p>
<p class='a_slot_pink'>Pink</p>
<p class='a_slot_orange'>Orange</p>
<p class='a_slot_4'>4</p>
<p class='a_slot_16'>16</p>
<p class='a_slot_16 other_class'>16</p>
<p class='a_slot_orange other_class'>Orange</p>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
depperm
  • 10,606
  • 4
  • 43
  • 67

4 Answers4

2

With your selector [class^='a_slot_'] you only select elements whose class attribute starts with that string so you wont select first three elements so you can change that to [class^='a_slot'] and after that you can split class attribute for each element and use some() to find match.

$("[class^='a_slot']").each(function(i, v) {
  var cls = $(this).attr('class').split(' ')
  var check = cls.some(function(e) {
    return e.startsWith('a_slot_') && !e.endsWith('green') && !e.endsWith('pink')
  })
  if (check) $(this).css('background', 'red')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class='a_slot a_slot_green'>Green</p>
<p class='a_slot a_slot_pink'>Pink</p>
<p class='a_slot a_slot_orange'>Orange (shouldn't I be highlighed too?)</p>
<p class='a_slot_green'>Green</p>
<p class='a_slot_pink'>Pink</p>
<p class='a_slot_orange'>Orange</p>
<p class='a_slot_4'>4</p>
<p class='a_slot_16'>16</p>
<p class='a_slot_16 other_class'>16</p>
<p class='a_slot_orange other_class'>Orange</p>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • You can also just use two attribute selectors, which is probably more efficient (and can be ported to CSS). See the duplicate link. – BoltClock May 23 '17 at 02:59
1

When you use:

$("[class^='a_slot_']") 

you select only elements that starts with a_slot_, but the third element is start with a_slot (without the following _).

For that you should use $("[class*='a_slot_']")

amhev
  • 313
  • 1
  • 3
  • 12
1

you can use jQuery.filter for that

$("[class^='a_slot']").filter(
     function(){
      var matches = $(this).attr('class').match(/pink|green/g);
           return !matches;
     }
).css('background-color', 'red');

Ah, yes as others pointed out your selector is too greedy I've removed the underscore

https://jsfiddle.net/py89zr5y/1/

andrew
  • 9,313
  • 7
  • 30
  • 61
0

Make it like this because ^ indicates start of string.

<p class='a_slot_orange a_slot'>Orange (shouldn't I be highlighed too?)</p>
error404
  • 331
  • 1
  • 8