...

View Full Version : Accessing a cell in a nested table



ken_shoti
05-06-2007, 11:20 AM
I want to access a particular cell in a table...telling javascript to click that cell which has a corresponding action...


<script>
document.write('<table id=t1>')

for (i=0;i<5;i++){
document.write('<tr><td onclick=action()></td><td>...</td></tr>')
}

document.write('</table>')

t1.rows[0].cells[0].click() //click cell 1 of row 1
</script>


and i would use this code for that....

these are only samples..each rows are actually created in a loop...but what if....


<script>
document.write('<table id=t1>')

for (i=0;i<5;i++){
document.write('<tr><td onclick=action()><table><tr><td onclick=action()></td></td></tr></table></td><td>...</td></tr>')
}

document.write('</table>')
</script>


that table i need is nested under the table t1....i cant use ids here since i'm doing a loop there..so is there a solution to achieve the clicking on a cell done on the former code?

i want this time to click cell 1 of row 1 IN table t1 row 1 cell 1

Thanks!

david_kw
05-06-2007, 04:47 PM
There is probably more than one way to do it. One is to use getElementsByTagName('table') on the cell you are interested in. Here is an example of that working.



<script>

function action(e, s) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
alert("cell clicked: " + s);
}
document.write('<table id=t1>')

for (i=0;i<5;i++){
document.write('<tr><td onclick="action(event, '+i+')"></td><td>...</td></tr>')
}

document.write('</table>')

document.write('<button onclick="t1.rows[0].cells[0].onclick()">Do it</button>'); //click cell 1 of row 1
</script>
<br><br><br><br>
<script>
document.write('<table id=t2>')

for (i=0;i<5;i++){
document.write('<tr><td onclick="action(event, '+i+')"><table><tr><td onclick="action(event, \'nested\'+'+i+')"></td></td></tr></table></td><td>...</td></tr>')
}

document.write('</table>')

document.write('<button onclick="t2.rows[0].cells[0].getElementsByTagName(\'table\')[0].rows[0].cells[0].onclick()">Do it</button>'); //click cell 1 of row 1 in nested table
</script>


I suspect there is a "better" way but it's too early to try and think of it right now. :)

david_kw

ken_shoti
05-07-2007, 11:59 AM
hey, thanks... didn't thought of that haha...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum