Initial state is that I have an array of ids [45, 3, 678] and a div tag with lots of elements inside, they are nested and all of them have a unique ids. I need to add class "selected" only for those elements, whose ids are in the array of ids. How can I achieva that?
<div id="parent-class">
<details id=1>
<ul id=2>
<details id=3></details>
<details id=4></details>
<details id=5></details>
...
</ul>
</details>
<details id=56>
<ul id=57>
<details id=58></details>
<details id=59></details>
<details id=60></details>
...
</ul>
</details>
</div>
$('div#parent-class').ready(function () {
var options = [45, 3, 678];
/* Here I need to loop through all children and assign class for those elements whose id is in array options*/
$('div#parent-class').children().each(function(){
var id = $(this).attr('id');
if ($.inArray(id, options)) $(this).addClass('selected');
});
});
Any help is very appreciated