Nizam Ali
Nizam Ali

Reputation: 241

get total number of rows of a particular class using javascript

I hope this should work. But, I don't know why its not working. Actually I'm trying to find the number of rows in a table which are having "active"class .

var p_rcd = document.getElementById('tblAllMessages').
        getElementsByTagName("tbody")[0].
        getElementsByTagName("tr").                             getElementsByClassName('active').length;

getElementsByClassName returns an array of elements. Am I going in wrong direction?

Upvotes: 0

Views: 91

Answers (2)

Silz
Silz

Reputation: 256

Try the below:

 var table = document.getElementById("tblAllMessages").getElementsByTagName("tbody")[0].getElementsByClassName('active').length;
  alert(table);

Upvotes: 0

Dastagir
Dastagir

Reputation: 1012

var rows = document.getElementById("tblAllMessages").getElementsByTagName("tbody")[0].getElementsByClassName("active").length;

alert(rows);

http://jsfiddle.net/92uuC/

Upvotes: 2

Related Questions