user3407440
user3407440

Reputation: 183

Show total no. rows for in HTML <span>

I need your help in displaying the total number of rows for the available tables in the appropriate span. There are 3 tables and I want the below code to show the total no. of rows in the first table in span1 & the total no. of rows for the second table in span2. The JavaScript is:

function count() {        
    var tables = document.getElementsByClassName("tablesorter");        
    var rows;        
    var span = Array();                 

    for (var i = 0; i < tables.length; i++) {        
        rows = tables[i].rows.length-1;        
        alert(rows);        
        alert(span[i]);        
        span[i].innerHTML = rows;        
    }

} 

However, span[i] is not picking up the value and print it in the HTML span. The HTML code is:

<body>
    <span id="span1"></span>
    <span id="span2"></span>
    <span id="span3"></span>
</body>

I am looking forward your assistant and help.

Upvotes: 0

Views: 194

Answers (1)

Pablo Matias Gomez
Pablo Matias Gomez

Reputation: 6813

Use this function instead:

function count() {

    var tables = document.getElementsByClassName("tablesorter");

    var rows;

    for (var i = 0; i < tables.length; i++) {

        rows = tables[i].rows.length-1;

        alert(rows);

        document.getElementById("span" + (i + 1)).innerHTML = rows;

    }
} 

You are never referring to the span, so that is why I used

document.getElementById("span" + (i + 1)).innerHTML = rows;

Upvotes: 1

Related Questions