Get the id of an HTML table, using a loop


I have a div, in which I will create an indeterminate number of tables which will have a non-numeric id, once these tables have been created I would like to go through them one by one and get their id as I will use it for later processes.

Is there any way to get the id of the table, either by JavaScript or by using the JQuery library? This is part of my script:

        var div = document.getElementById('divTablas'); //un div que contiene un número  n de tablas

        for (var i = 1; i <= div.getElementsByTagName('table').length; i++) {

         var idTabla = //como obtengo el id de la tabla que está recorriendo mi bucle?

Or is there some way to get an array with the id of the tables in my DIV, and then go through the array?

asked by Carlos Daniel Zárate Ramírez 23.06.2018 в 21:52

2 answers


You can ask in jQuery for the attribute id . It is very easy to travel with each from a selector such as a common class.

$(".clase").each(function (){
answered by 23.06.2018 / 22:07

You can get all the classes with querySelectorAll and run it with for

let divs = document.querySelectorAll(".clase");
for (let i = 0; i < divs.length; i++) {
  let id = divs[i].id;
  console.log("resultado :", id);
<div id="uno" class="clase">hola 1</div>
<div id="dos" class="clase">hola 2</div>
<div id="tres" class="clase">hola 3</div>
<div id="cuatro" class="clase">hola 4</div>
<div id="cinco" class="clase">hola 5</div>
answered by 24.06.2018 в 02:44