-6

So lets say I have an element that contains this:

Image Of Children

How would I be able to run through all these children in javascript?

smac89
  • 39,374
  • 15
  • 132
  • 179
Voided
  • 3
  • 3
  • 3
    Possible duplicate of [How can I iterate through an Elements child nodes using Javascript?](https://stackoverflow.com/questions/16672647/how-can-i-iterate-through-an-elements-child-nodes-using-javascript) OR [How to select all children of an element with javascript and change CSS property?](https://stackoverflow.com/questions/9780443/how-to-select-all-children-of-an-element-with-javascript-and-change-css-property) – smac89 Mar 28 '18 at 01:11

2 Answers2

0

To iterate over tables seems not useful but here it is. Note I am using the class name for your tables above to find each one. If your example had different classes for each table this would not work.

var tables = document.getElementsByClassName("table");
var arr = new Array();

for (i = 0; i < tables .length; i++) {
    //do what you want here
}

If you want to work with the rows and cells within a table you might want to refer to this solution.

mcv
  • 1,380
  • 3
  • 16
  • 41
0

Use querySelectorAll (which can be directly iterated over, unlike the HTMLCollection methods):

document.querySelectorAll('table.table').forEach(table => {
  // do something with each table
});
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320