Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Select element in jQuery when one of two possible .data attributes has the desired value

My question is rather simple but I couldn't find an existing answer:

I want to select a subset of elements if one of their two possible data attributes is "X".

My selector looks currently like this:

$('.line').data('identifier1', 'X');

I would like to add to the same selector .data('identifier2', 'X') using an OR statement, if that's possible with jQuery?

So the result should be that all .line elements are chosen if they have the value 'X' in either their data-identifier1 or data-identifier2.

Here is a snippet with only the OR statement missing:

let myCollection = $('.line').data('indentifier1', "X");

console.log(myCollection);

//myCollection should get divs 1,3,4 but not 2
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="line" data-identifier1="X" data-identifier2="Y">1</div>
<div class="line" data-identifier1="Y" data-identifier2="Y">2</div>
<div class="line" data-identifier1="Y" data-identifier2="X">3</div>
<div class="line" data-identifier1="X" data-identifier2="X">4</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use each() and a array to store each div inside

let DivsLine = $('.line');
let myCollection = new Array();

$(DivsLine).each(function(index, value){
  let identifier1 = $(this).data('identifier1');
  let identifier2 = $(this).data('identifier2');
  
  if (identifier1 == 'X' || identifier2 == 'X') {
    myCollection.push($(this).text());
  }
});

console.log(myCollection);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="line" data-identifier1="X" data-identifier2="Y">1</div>
<div class="line" data-identifier1="Y" data-identifier2="Y">2</div>
<div class="line" data-identifier1="Y" data-identifier2="X">3</div>
<div class="line" data-identifier1="X" data-identifier2="X">4</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can query data attributes like this and use , as an or.

 let myCollection = $('.line[data-identifier1="X"],.line[data-identifier2="X"]');
 console.log(myCollection);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="line" data-identifier1="X" data-identifier2="Y">1</div>
<div class="line" data-identifier1="Y" data-identifier2="Y">2</div>
<div class="line" data-identifier1="Y" data-identifier2="X">3</div>
<div class="line" data-identifier1="X" data-identifier2="X">4</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!