To access column number 3 you only need a few changes. First you need to add id for the table:
<table id="table1" ...>
Second change of JavaScript code to:
var table1 = document.getElementById("table1") for(var i = 0; i < table1.rows.length; i++) { var cell_value = table.rows[i].cells[3].textContent; }
Then you can use cell_value
or just use document.getElementById("table1").rows[i].cells[j].textContent and i will be your desired row and j your column (in this case, number 3 ):
document.getElementById("table1").rows[0].cells[3].textContent
To avoid the header and footer (if <thead> or <tfoot> specified in the table), you need to change the loop:
for(var i = 1; i < table1.rows.length - 1; i++) { ...
i = 1 for the header, table1.rows.length - 1 for the footer.
If you cannot add id="" to the table, you can try using this JavaScript code:
var cell_value = document.getElementsByTagName("table")[0].rows[0].cells[3].textContent;
document.getElementsByTagName("table")[0] is your table with index [0] , so this is the first table in the document body, then rows[0] means that this is the first row, and cells[3] means that it the fourth column, while textContent will textContent data from the cell for you.
Hope this helps.
source share