javascript - Count number of elements in the html table -
what approach should use count number of elements in html table.?
for eg: if have 3*3 table , need count number of a's , b's
b b b
this javascript function retrive content of each cell , after getting content can write own logic on that
<script> function getcellvalues() { var reftab = document.getelementbyid("your_table_id") var ttl; // loop through rows , columns of table , popup alert value // /content of each cell. ( var = 0; row = reftab.rows[i]; i++ ) { row = reftab.rows[i]; ( var j = 0; col = row.cells[j]; j++ ) { alert(col.firstchild.nodevalue); } } } </script>
and below code fulfill requirement
<script> function getaandbcount() { var reftab = document.getelementbyid("your_table_id") var ttl; var a=0; var b=0; ( var = 0; row = reftab.rows[i]; i++ ) { row = reftab.rows[i]; ( var j = 0; col = row.cells[j]; j++ ) { if(col.firstchild.nodevalue == 'a'){ a++; }else if(col.firstchild.nodevalue == 'b'){ b++; } } } alert(a); alert(b); } </script>
Comments
Post a Comment