1

How can I select element with particular data attribute and special class with jQuery please ?

For example, I would like to select these elements:

<div data-roomid="55" data-status="NoData"></div>
<div data-roomid="55" data-status="Open"></div>
<div data-roomid="55" data-status="Close"></div>

But not this one:

<div data-roomid="55" data-status="Book"></div>

My try is the following one:

roomid = 55;
$('[data-roomid="'+ roomid +'"]').each(function(e) {

});

Thanks.

kukkuz
  • 41,512
  • 6
  • 59
  • 95
Testy
  • 301
  • 2
  • 9

2 Answers2

1

You can use :not selector - see demo below:

var roomid = 55, status = 'Book';
$('[data-roomid="' + roomid + '"]:not([data-status=' + status + '])').each(function() {
  $(this).css({
    color: 'blue'
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-roomid="55" data-status="NoData">NoData</div>
<div data-roomid="55" data-status="Open">Open</div>
<div data-roomid="55" data-status="Close">Close</div>
<div data-roomid="55" data-status="Book">Book</div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
0

Is this what you're looking for?

$('div:not([data-status=Book])[data-roomid='+ roomid +']')
alwayslearning
  • 268
  • 2
  • 9