You can use IDs of the matched elements.
var $c = $("input[type=checkbox]");
$("div[id^=count]:contains(0)").css('color', 'red').each(function(i) {
var id = this.id.replace('count', 'chkbox');
$c.filter('[id="'+ id +'"]').prop('checked', true);
});
In case that you want to select elements that their text content is just 0
and not 10
, 20
, ... you can use filter
method:
$("div[id^=count]").filter(function(){
return $.trim( $(this).text() ) === '0';
}).css('color', 'red').each(function(i) {
var id = this.id.replace('count', 'chkbox');
$c.filter('[id="'+ id +'"]').prop('checked', true);
});