Is there a way to access an array of col labels for a table in javascript?

I have a table that looks like this:

<table id="myTable"> <col id="name" /> <col id="birthYear" /> <col id="phone" /> <td> <tr>Joe</tr> <tr>1972</tr> <tr>202-555-1234</tr> </td> </table> 

Is there an easy way to get an array of <col /> tags? I do not want to use getElementById because I do not know the col tag identifiers, although I will know the table Id. I do not want to use getElementsByTagName because there will be several tables with col tags in the document.

I do not use jquery, just plain javascript.

Any ideas?

+6
source share
2 answers

getElementsByTagName can be used in the table with the identifier "myTable": it will return all col of this table.

here is an example:

 document.getElementById("myTable").getElementsByTagName("col") 
+5
source

Javascript supports XPath:

 document.evaluate('//col', document.getElementById('myTable')); 
+4
source

Source: https://habr.com/ru/post/894650/


All Articles