Kurkula
Kurkula

Reputation: 6762

Jquery selecting check box by click on text in a table td

I am trying to check and uncheck checkbox by clicking on text next to it. Here is the example in which i am trying to click on item name and checkbox should be selected. I think toggle should fit here but I am unable to make it work.

Jsfiddle

$("#checkall").change(function () {
  $(this).closest("table").find(".checkbox").attr("checked", this.checked).change();
});

$(".checkbox").change(function() {
  $(this).closest('tr').toggleClass("highlight", this.checked);
});

Upvotes: 0

Views: 609

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

I think you are targeting the fileName, if so then try

$("#checkall").change(function () {
    $(this).closest("table").find(".checkbox").prop("checked", this.checked).change();
});

$(".checkbox").change(function () {
    $(this).closest('tr').toggleClass("highlight", this.checked);
});

$('.filename').click(function () {
    $(this).closest('tr').find('input').prop('checked', function (i, c) {
        return !c;
    }).change()
})

Demo: Fiddle

  • have a click handler for the td with the target text
  • find the input element in the same row and toggle its checked state

Upvotes: 2

Related Questions