Hi , I'm trying to count the rows of a table html
and show them with a label
but I can't find how to do it, I found a way to do it with javaScript
but the function calls it by means of a onclick
like this:
<html>
<head>
<title>Ejercicio</title>
</head>
<script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
<script>
function contarFilas() {
var $num = document.getElementById('tblUsuario').getElementsByTagName('tr').length - 1;
alert($num);
}
</script>
<body>
<table border="1" id="tblUsuario">
<tr>
<th>Nombre</th>
<th>Apellido</th>
</tr>
<tr>
<td>Hinata</td>
<td>Hyuga</td>
</tr>
<tr>
<td>Yuno</td>
<td>Gasai</td>
</tr>
<tr>
<td>Asuna</td>
<td>Yuuki</td>
</tr>
</table>
<input type="submit" value="Enviar" onclick="contarFilas()"/>
</body>
</html>
But as I say, I'm not looking for the number of rows to come out like this, what I'm looking for is for it to come out in a label above the table. How could I do it?
Thanks.
If you only have one table you can count the elements
tr
, as in this example. If there are more tables or you only want to count certain rows, you can add different classes to themtr
and count the amount of each element, as I added in the seconddiv
.