简体   繁体   中英

Get data out of html table cells

I have a huge html table that I am building dynamically, and I want to be able to get cell data out of it easily.
Here is how my table is set up.

   <tr><td  id="presCode0">V</td></tr>
    <tr><td  id="presCode1">F</td></tr>

Each row element is numbered when I build my table. Each row has 5 cells and there are hundreds of rows in the table. Each row when clicked calls a function like this:

onclick="switchToRequest(rownumber)"    

I have tried using:

function switchToRequest(i)
{
var presCode=''+'presCode' + i + '';
attend.elements["codePick"].value=presCode.innerHTML;
attend.elements["codePick"].value=$("#"+"presCode"+i);
}

is there any way that I can get table cell values out of a complex table using Javascript?

Try this:

attend.elements["codePick"].value = $("#"+"presCode"+i).html();

or

attend.elements["codePick"].value = $("#"+"presCode"+i).text(); // to get text minus html tags

Replace

presCode.innerHTML;

with

$("#"+presCode).innerHTML;

and remove the second attend.elements line

function switchToRequest(i)
{
var presCode=''+'presCode' + i + '';
attend.elements["codePick"].value=document.getElementById(presCode).innerHTML;
attend.elements["codePick"].value=$("#"+"presCode"+i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM