grid0X divs :
var countOnes = 0;
$('div[id^=grid0]').each(function() {
if ($(this).text() === "1") {
++countOnes;
}
});
div, grid0 (, grid00, grid01 ..). , "1", , ; , .
Similarly, for grid1Xjust change the selector to use 1instead 0.
Alternatively, however, if these divs are in some kind of container, you can use the selector to find the container and then skip its children, for example:
HTML:
<div id="grid0">
<div>0</div>
<div>0</div>
<div>0</div>
<div>1</div>
<div>0</div>
<div>0</div>
</div>
JavaScript:
$("#grid0 > div").each(...);
... and avoid having all of these identifiers.
source
share