Javascript table help

hello!

i’ll go straight to the point, i have 8 rows and 3 columns, i want to populate each <td> starting from:
row 0 column 0 a value of 1
row 0 column 1 a value of 2
row 0 column 2 a value of 3
row 1 column 0 a value of 4
row 1 column 1 a value of 5

row 7 column 2 a value of 24

also if i choose say row 2 column 2 as starting point the rest of the <td>'s before it will not contain any value…

any help from the experts is really appreciated!

here’s the code


<script type="text/javascript">
	function popIt() {
		var t = document.getElementById('tblVisaStk').rows;
		//var t = document.getElementById('tblVisaStk');
		var k = getCookie('matrix'); // starting point, set in a different file
		var r = k[1]; // e.g. from R2C1, extract coordinates row 2
		var c = k[3]; // column 1
		var col;
		for(i = r; i < 8; i++) {
                        // can't get it to work
			//if (c == 1) {	col = 0; } else	if (c == 2) { col = 2; } else { col = 0; }
			for (j = c; j < 3; j++) {
				if (i == r) { j++; }
				//if (k == 'R'+i+'C'+j) {
					var y = t[i].cells;
					y[j].innerHTML = i+j;
				//}
				/*var oCell = t.rows(i).cells(j);
				oCell.innerHTML = 'test';*/
			}
		}
		/*var oTable = document.getElementById("tblVisaStk");
		var oCell = oTable.rows(2).cells(1);
		oCell.innerHTML = 'test';*/
	}
</script>

There are a few errors in your script.

Try this and see how it works for ya.

I’m assuming that r=1, and c=1 (per your cookie) refers to the first cell of the first row in the table (t.rows[0].cells[0]).

Also assuming that if you start at r=2, c=2, you don’t want ANY of the first cell to be filled… so you’ll fill a 2x8 block (16 cells total) and the first column will be empty.

If you just want 24 cells filled, wrapping around to full rows, that will be a different solution.


<script type="text/javascript">
    function popIt() {
        var t = document.getElementById('tblVisaStk');
        var k = getCookie('matrix'); // starting point, set in a different file
        var r = k[1]; // e.g. from R2C1, extract coordinates row 2
        var c = k[3]; // column 1
        var col;
        for(var i = r-1; i < r+6; i++) {
            for (var j = c-1; j < 3; j++) {
                var val = (i-r+1)*3 + j-c+1;
                if (t.rows.length > i) {
                    t.rows[i].cells[j].innerHtml = val;
                }
            }
        }
    }
</script>

thank you for your reply.

i tried your code with cookie value at row 0 and col 0 and got an error
‘rows[…].cells’ is null or not an object
but for other row values and column values didn’t display anthing.

Did some testing and got it working… apparently innerHtml doesn’t wanna take!!!
This should do it


<script type="text/javascript">
    function popIt() {
        var t = document.getElementById('tblVisaStk');
        var k = getCookie('matrix'); // starting point, set in a different file
        var r = k[1]; // e.g. from R2C1, extract coordinates row 2
        var c = k[3]; // column 1
        var col;
        for(var i = r; i < r+8; i++) {
            for (var j = c; j < 3; j++) {
                if (t.rows.length > i) {
					if (t.rows[i].cells.length > j) {
						//t.rows[i].cells[j].innerHtml = (i-r)*3 + j + 1;
						t.rows[i].cells[j].appendChild(document.createTextNode(''+((i-r)*(3-c) + (j-c) + 1)));
					}
                }
            }
        }
    }
</script>

[/QUOTE]

[/QUOTE]

thank you very much Steve, i did some testing too, now it’s displaying…:wink:
unfortunately, this is what it’s supposed to do, please extend your patience more :smiley:

| x | x | x |
| x | 1 | 2 |
| 3 | 4 | 5 |
…etc…

x = blank

thanks Steve, I just can’t get it to work…

Here you go… just change the “24” to whatever number you want for the # of items to display…


<script type="text/javascript">
    function popIt() {
        var t = document.getElementById('tblVisaStk');
        var k = getCookie('matrix'); // starting point, set in a different file
        var r = k[1]; // e.g. from R2C1, extract coordinates row 2
        var c = k[3]; // column 1
        var cols = 24;
        for(var i = 0; i < cols; i++) {
            var x = (i+r*3+c) % 3;
            var y = Math.floor((i+r*3+c)/3);
            if (t.rows.length > y) {
                if (t.rows[y].cells.length > x) {
                    t.rows[y].cells[x].appendChild(document.createTextNode(''+(i+1)));
                }
            }
        }
    }
</script>

thanks steve, really appreciate it, no data is displayed? :sick:

hey steve, thank you! i finally did it! :rofl:

<script type="text/javascript">
	function popIt() {
		var t = document.getElementById('tblVisaStk');
		var k = getCookie('matrix'); // starting point, set in a different file
		var r = k[1]; // e.g. from R2C1, extract coordinates row 2
		var c = k[3]; // column 1
		var cnt = 0;
		for(var i = 0; i < 8; i++) {
			for (var j = 0; j < 3; j++) {
				t.rows[i].cells[j].appendChild(document.createTextNode((i < r || (i == r && j < c))? '' : cnt++));
			}
		}
	}
</script>

@transio

I think you wanted: innerHTML

Note:
innerHtml - won’t work