irayan05 irayan05 - 11 months ago 82
Javascript Question

Javascript : How to get text value of specific columns in a table for each row?

I have a simple HTML table that can contain 2-5 rows and 6 columns each. How can I get the value of 1 and 2 columns for each row? I would also like to display it in a output like this:

Column1 Column2, Column1 Column2, Column1 Column2
would be the output if it has 3 rows.

EDIT: Created a jsfiddle to describe what Im trying to do and also the sample code where I am at so far https://jsfiddle.net/y6eoc0b4/

Answer Source

You can do this

var myTable = document.getElementById("myTable");
var rows = myTable.getElementsByTagName("tr");
console.log(rows);
var output = [];
for(var i = 0; i< rows.length; i++){

    var cells = rows[i].getElementsByTagName("td");
  output.push(cells[0].innerText+" "+cells[1].innerText)
}
document.getElementsByClassName("output")[0].innerText = output;

https://jsfiddle.net/LyswLtf8/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download