How to get data from table columns in HTML

<table border="1" cellpadding="2" cellspacing="0" width="75%" bordercolor="#000000"> <tbody><tr bgcolor="mediumblue"> <td width="20%"><p align="center"><font face="Arial" color="white" size="2"><strong>SUB CODE</strong></font></p></td> <td width="26%"><p align="left"><font face="Arial" color="white" size="2"><strong>SUB NAME</strong></font></p></td> <td width="13%"><p align="left"><font face="Arial" color="white" size="2"><strong>THEORY</strong></font></p> </td> <td width="10%"><p align="left"><font face="Arial" color="white" size="2"><strong>PRACTICAL</strong></font></p> </td> <td width="17%"><p align="left"><font face="Arial" color="white" size="2"><strong>MARKS</strong></font></p></td> <td width="14%"><p align="center"><font face="Arial" color="white" size="2"><strong>GRADE</strong></font></p></td> </tr> <tr bgcolor="#ffffff"> <td align="middle"><font face="Arial" size="2"> 042</font></td> <td align="left"><font face="Arial" size="2">PHYSICS</font></td> <td align="left"><font face="Arial" size="2">027</font></td> <td align="left"><font face="Arial" size="2">028</font></td> <td align="left"><font face="Arial" size="2">055&nbsp;&nbsp;&nbsp;&nbsp;</font></td> <td align="middle"><font face="Arial" size="2">D1</font></td> </tr> <tr bgcolor="#e6e6fa"> <td align="middle"><font face="Arial" size="2">043</font></td> <td align="left"><font face="Arial" size="2">CHEMISTRY</font></td> <td align="left"><font face="Arial" size="2">026</font></td> <td align="left"><font face="Arial" size="2">029</font></td> <td align="left"><font face="Arial" size="2">055&nbsp;&nbsp;&nbsp;&nbsp;</font></td> <td align="middle"><font face="Arial" size="2">D2</font></td> </tr> <tr bgcolor="mediumblue"> <td>&nbsp;</td> <td colspan="5"><b><font face="Arial" size="2" color="white">Result:&nbsp;&nbsp; XXXX <!-- : --> </font></b> <b><font face="Arial" size="2" color="white"> &nbsp;</font></b></td> </tr> </tbody></table> 

I have a piece of code that creates a table. I want to extract MARkS from a table and store it in an array.

 document.getElementsByTagName("table")[0].cells[3] 

gives the result as UNDEFINED

So how can I do this?

NOTE. I cannot edit html pages.

+5
source share
4 answers

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.

+2
source
 document.getElementsByTagName("table") 

returns an array of DOM elements. It is difficult to determine a table from a list of arrays.

the best way to select a table is to associate an identifier with it and use document.getElementById

 <table id="my-table" border="1" cellpadding="2" cellspacing="0" width="75%" bordercolor="#000000"> 

JavaScript:

 document.getElementById("my-table"); 

In addition, you need to specify the lines in front of the cells to select a specific .ie cell for the first cell

 document.getElementById("my-table").rows[0].cells[0] 
+1
source

This solution is the job:

 table.find('tr').each(function (i, el) { var $tds = $(this).find('td'), productId = $tds.eq(0).text(), product = $tds.eq(1).text(), Quantity = $tds.eq(2).text(); // do something with productId, product, Quantity }); 
0
source

You can put the table and cells in different variables for easy debugging. In this example, you need to define an identifier name for your table, for example, <table id="tbl">

 var table = document.getElementByID("tbl"); var d = table.getElementsByTagName("tr")[0], var r = d.getElementsByTagName("td")[0]; 
0
source

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


All Articles