Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable disable HTML table element with JQuery in disabled <TR>

I am creating a dynamic HTML table with JQuery. Sample code is below.

var tbody=$("#myTable tbody");

var tableRow;

if(somevariableIsTrue)
   tableRow=$("<tr>").attr("disabled",true);
else
   tableRow=$("<tr>");

var td=$("<td>");
    td.appendTo(tableRow);

var tdn=#("<td>");
tdn.appendTo(tableRow);

 tableRow.appendTo(tbody);

now I create different TDs and append to the tableRow. If the disabled attribute of the row is TRUE obviously all the TDs for that row will be disabled too. But I want that my first TD of disabled row shouldn NOT be disabled as its a checkbox column on its click I want to enable/disable the same row. I tried different ways to get the checkbox in the first column and tried to enable it but all fails. Can some one suggest me how to do this in JQuery

EDIT Hope this screen shot helps to understand my requirement enter image description here

as seen in the image first row is disabled including the first td with checkbox. I want to checkbox column to be enabled all the time so that on checking it the row becomes enabled and un checking leaves the row disabled again.

like image 714
V.B Avatar asked Nov 01 '22 17:11

V.B


2 Answers

Here is a simple solution:

// first row checkboxes
$('tr td:first-child input[type="checkbox"]').click( function() {
   //enable/disable all except checkboxes, based on the row is checked or not
   $(this).closest('tr').find(":input:not(:first)").attr('disabled', !this.checked);
});

See it working in this JSFiddle

like image 177
DDan Avatar answered Nov 13 '22 18:11

DDan


You could just call

$('input[type=checkbox]').removeAttr('disabled');

or

$('input[type=checkbox]').prop('disabled', false);

on all your checkboxes. This would essentially re-enable all of your checkboxes

like image 37
Rich Avatar answered Nov 13 '22 17:11

Rich