Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a table dynamically. Each row's last cell is a checkbox (see the code below).
var chk = document.createElement('input');
     chk.setAttribute('type', 'checkbox');
     var cell = row.insertCell(-1);
     cell.appendChild(chk);

Using the piece of code below:
this.dvTbl.on('click', 'tr', function() {
     var v = this.cells[1].innerHTML;
     var v2 = this.cells[2];  // a checkbox in this cell
     // add code here

When I click a checkbox, it checked on/off. How can I detect a checkbox's css? e.g. to detect it is on or off? Thanks.

What I have tried:

Can find some code for detecting a checkbox, but not the checkbox in a table's cell.
Posted
Updated 1-Jun-21 22:21pm

1 solution

If you're using vanilla Javascript you can select the first input element from the cell and check the checked state like so:

JavaScript
var v2 = this.cells[2];
var checkbox = v2.getElementsByTagName("input")[0];
var checked = checkbox.checked;

Alternatively you can just look at the children[^] property instead, though I find it better to be explicit when looking up elements.
 
Share this answer
 
Comments
s yu 2-Jun-21 7:34am    
PERFECT answer! Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900