My question is about arrays and its verification. The following is necessary.

There are data-id="38383" on the page with the data-id="38383" , and these id always different.

What you need : write in the array defined id . Next, we check for the presence of these div with the data-id attributes specified in the array and perform adding a class to this block with the specified id .

Looks like that:

 <div class="pr" data-id="10230"></div> 
  • Where is your attempt to implement the code? - Vladimir Gonchar
  • Yes, I try it like this, but I don’t understand what I’m doing wrong) - Dmitriy
  • @ Dmitry, how are these "certain" divas defined? - CbIPoK2513
  • @ CbIPoK2513, put the id list in the array, then look for the .pr class on the page with the data-id if the id in the array with the data-id matches, add the class with that id with which the additional class coincided - Dmitry

2 answers 2

From comment
@ CbIPoK2513 , enter the id list in the array, then look for the .pr class on the page with the data-id if the id in the array with the data-id matches, add the class with that id with which the additional class coincided

 let prArr = [ '1', '2', '4', '5' ]; for(var i=0; i<prArr.length; i++) { let elem = $('.pr[data-id="'+prArr[i]+'"]'); // объявляем объект if(elem.length > 0){ // Если объект существует elem.addClass('asd'); // Добавляем ему класс } // Ps // Указав в цикле условие // if(elem.length == 1) { // elem.eq(0).addClass('asd'); // } // Можно избежать таких моментом, если на странице, допустим случайно, существуют больше одного одинаковых элементов. } 
 .asd {color: green;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="pr" data-id="1">block 1</div> <div class="pr" data-id="2">block 2</div> <div class="pr" data-id="2">block 2</div> <!-- допустим такой вариант, что элементов с data-id="2" - две штуки. --> <div class="pr" data-id="3">block 3</div> <!-- Пропустим 4 --> <div class="pr" data-id="5">block 5</div> 

Differences from the answer @Vladimir
The search goes not all elements with class pr , but specifically those that are specified in the array.
Well, the code is written on JQ, as it was indicated in the question mark)

  • one
    super, thank you very much! - Dmitriy

Something of this type?

 var red = ['10230', '10236']; document.querySelectorAll('.pr').forEach(function(div) { if (red.includes(div.dataset.id)) { div.classList.add('red'); } }); 
 .red { background-color: #f00; } 
 <div class="pr" data-id="10230">10230</div> <div class="pr" data-id="10231">10231</div> <div class="pr" data-id="10232">10232</div> <div class="pr" data-id="10233">10233</div> <div class="pr" data-id="10234">10234</div> <div class="pr" data-id="10235">10235</div> <div class="pr" data-id="10236">10236</div>