5

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.

12
  • 5
    The tr element doesn't have a disabled attribute. If you want to disable the inputs within that tr, you need to add the attribute to all of them individually. Commented Sep 11, 2014 at 8:41
  • Well it works with tr and disables all the other elements in that tr as required. I do not want to iterate each td to enable disable the elements so I chose this method. Commented Sep 11, 2014 at 8:43
  • You should also consider a lightweight templating engine. Commented Sep 11, 2014 at 8:44
  • 1
    @V.B is this fiddle what you want? Commented Sep 11, 2014 at 8:49
  • 1
    @V.B you claim disabled works on <tr>. Beware this does not work on modern browsers. I suppose you're using an old version of IE. Instead you should disable the form elements that are children of that table row. Commented Sep 11, 2014 at 9:16

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.