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.

5
  • What is a special class? The term class has special meaning in html, so it's a bit confusing what you want. Commented Apr 2, 2019 at 1:38
  • stackoverflow.com/questions/2487747/… Commented Apr 2, 2019 at 1:38
  • One of the divs you want to select is identical to one you don't. Commented Apr 2, 2019 at 1:39
  • @kukkuz: good catch. Updated. Commented Apr 2, 2019 at 1:40
  • @j08691, correct. Changed. Commented Apr 2, 2019 at 1:40

2 Answers 2

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>

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

Comments

0

Is this what you're looking for?

$('div:not([data-status=Book])[data-roomid='+ roomid +']')

Comments

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.