Your attempt to use the for-in statement in your script actually iterates over all the properties of the jQuery array object:
for (var id in $('#Root div.ListItem')) {
You do not want this; you need to iterate over the elements in an array-like object:
for (var id in $('#root span').toArray()) {
You will see above that the conclusion is what you expected.
So, back to your original question. It sounds like you just need to break out of the loop as soon as you find a match. If you are wondering how to exit the jQuery each loop, just return false; after setting found1 = true; . You should not be afraid to get a callback; this callback is only performed for each element of your selector in the usual old for-loop "under the hood".
If you really want to write a for-each structure yourself, then this will be enough:
var found1 = false; var items = $('#Root div.ListItem').toArray();
A shorter, but slower way to do this could be to use $.grep and check to see if something was found:
var found1 = $.grep($('#Root div.ListItem'), function() { return group == $(this).text(); }).length > 0;
I would not recommend the latter unless the selector returns only a few elements (e.g. <50).
source share