0

I'm trying to get the control which is inside of a cell table; in my table I have different controls, labels, checkboxes, etc.

I basically need to get the control which is used in that table

var x = document.getElementById('myTable').rows[0].cells;
alert(x[0].innerText);
//alert(x[3].innerHTML);

if (x.Control == checkbox) {

    x.checked = true;
}

This will be in a loop but for now I just need to be able to check the checkbox by grabbing the control and setting that control to true

Any hints/help would be great

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

I doesn't really understand what you exactly need is it

document.getElementById("checkbox").checked = true; 

here checkbox is the id of a particular checkbox

user2779544
  • 429
  • 1
  • 8
  • 24
  • I don't know what the ID of my checkbox will be as their dynamically created, I just either need to be able to get the control or get the ID –  Oct 08 '13 at 08:54
0

I would put a unique id on the form element instead and use that to grab it. In this way you can change the structure in the future. Example: perhaps you no longer want to use a table grid, but a grid of divs.

When you use innerHTML you will might also grab textnodes and other things you put in the cell.

An alternative - if you really want to find a specific cell in a table - is to give each cell a unique id on the form "cell-4-5" where 4 is the row and 5 is the column.

[EDIT]

If you want to have the cell contents returned as a DOM-object then childNodes can be used:

var x = document.getElementById('myTable').childNodes[0].childNodes[0].childNodes

If you want to have the cell contents returned as a string then innerHTML can be used:

var x = document.getElementById('myTable').childNodes[0].childNodes[0].innerHTML

To check if the checkbox is checked you need to keep it as a DOM element and thus use the first version.

mipmap
  • 221
  • 1
  • 13
  • I just need to get the control type from the cell, I don't want to change it from a table. –  Oct 08 '13 at 09:10