irayan05 irayan05 - 2 years ago 135
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

Answer Source

You can do this

var myTable = document.getElementById("myTable");
var rows = myTable.getElementsByTagName("tr");
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;

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