I have several divs in my UI with the same class. Each of them has a different data-id
.
I want to check if one of the divs has a specific value for the data-id
.
I can do it with .each()
but was wondering if there is a way not to iterate every time on all the divs.
If I find a div (by class and data-id
value) I need to manipulate its content. If I don’t find any div that satisfy the two criteria then I will perform some different activities.
What I have now (works but iterates every time the Dom)
let found = false;
$('.discussion').each(function(){
if($(this).data("id")==myVariable){
//I have found what I am looking for
//do my stuff
found = true;
}
});
if(found===false){
//I have not found what I was looking for
//so I will add the div with that class and data-id
}
There can be even more then 10 divs with that class and this event will fire even 100 times a minute so performance may be impacted.