Skip to content

Instantly share code, notes, and snippets.

@blizzardengle
Last active February 18, 2023 17:43
Show Gist options
  • Save blizzardengle/6c7ae6e4b10a7ce6f16731bfb4bcba06 to your computer and use it in GitHub Desktop.
Save blizzardengle/6c7ae6e4b10a7ce6f16731bfb4bcba06 to your computer and use it in GitHub Desktop.
Capture Table
/**
* Loop through a table and pull out certain cells of data.
*
* This loops through a the rows of a table and grabs the requested cell(s) from each row.
* Cell data is seperated with tabs when multiple cells of data are requested OR with the
* combine flag set to true the data will be combined into a single string per row.
*
* Version: Alpha
*
* @param {int} id The value of the tables HTML id attribute.
* @param {array} cells An array of cell numbers you would like; counting starts at 0!
* @param {bool} combine Set to true to combine multiple cells of data into a single string; default false.
*/
function captureTable( id, cells, combine ) {
var tbl = document.getElementById( id );
var tr = tbl.querySelectorAll('tr');
var td;
var rowCount = tr.length;
var cellCount = 0;
var cell;
var output = '';
for ( var row = 0; row < rowCount; row++ ) {
td = tr[ row ].querySelectorAll('td');
cellCount = td.length;
for ( cell = 0; cell < cellCount; cell++ ){
if ( combine ) {
if ( cells.indexOf( cell ) > -1 ) {
output += td[ cell ].innerText.trim();
}
} else {
if ( cells.indexOf( cell ) > -1 ) {
output += td[ cell ].innerText.trim() + '\t';
}
}
}
output += '\n';
}
console.log( output );
return output;
}
@128f
Copy link

128f commented Feb 18, 2023

worked as advertised, applied an id test to the table in question and did

captureTable("test", [2])

and got the column I wanted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment