Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  116] [ 6]  / answers: 1 / hits: 129267  / 12 Years ago, fri, may 11, 2012, 12:00:00

A user clicks on a row of a table, and I want to get (in Javascript) the innerhtml of let's say the 3rd column of that row.



Something like :



document.getElementById(tblBlah).rows[i].columns[j].innerHTML


doesn't seem achievable and I can't find anything here or in the net.



Any solutions would be very much appreciated ( NO jQuery )


More From » html

 Answers
41
document.getElementById(tblBlah).rows[i].columns[j].innerHTML;


Should be:



document.getElementById(tblBlah).rows[i].cells[j].innerHTML;


But I get the distinct impression that the row/cell you need is the one clicked by the user. If so, the simplest way to achieve this would be attaching an event to the cells in your table:



function alertInnerHTML(e)
{
e = e || window.event;//IE
alert(this.innerHTML);
}

var theTbl = document.getElementById('tblBlah');
for(var i=0;i<theTbl.length;i++)
{
for(var j=0;j<theTbl.rows[i].cells.length;j++)
{
theTbl.rows[i].cells[j].onclick = alertInnerHTML;
}
}


That makes all table cells clickable, and alert it's innerHTML. The event object will be passed to the alertInnerHTML function, in which the this object will be a reference to the cell that was clicked. The event object offers you tons of neat tricks on how you want the click event to behave if, say, there's a link in the cell that was clicked, but I suggest checking the MDN and MSDN (for the window.event object)


[#85650] Thursday, May 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxd

Total Points: 568
Total Questions: 100
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;