Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: open

Message Too Old, No Replies

how to access particular elements of a cell of a row of a html table

   
4:20 pm on Oct 24, 2011 (gmt 0)



Suppose i have a table wherein i have x rows. In each row there are y1 y2 y3 elements. How do I access each of them in Javascript.
6:25 pm on Oct 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Sayantandutta,

here is an example for you to try...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
#mytable {
border:2px outset #999;
margin:auto;
}
#mytable td {
padding:5px;
border:1px inset #999;
}
</style>

<script type="text/javascript">
function init(){
var tr=document.getElementById('mytable').rows;
for(var c=0;c<tr.length;c++) {
for(var k=0;k<tr[c].cells.length;k++){
tr[c].cells[k].firstChild.nodeValue='row '+c+' cell '+k;
}
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>

</head>
<body>

<table id="mytable"><tr>
<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr><tr>
<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr><tr>
<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
</tr></table>

</body>
</html>

birdbrain