1

I have a jquery function, that activates only when a table row is clicked and if so, it invokes controller method. However, this row also contains checkbox, and if i click it i don't want this method to be called. I tried checking the clicked element type or other parameters like class, but it seems to only apply to the entire row. Any ideas how to make it work?

JQuery:

function AllowTableRowsToBeClicked() {
    $('#pref-table tbody tr').click(function () {
        var resourceName = $(this).attr('title');
        var categoryName = $('#pref-table').attr('name');
        var url = "/Home/GetSpecific";
        $.post(url, { categoryName: categoryName, resourceName: myClass }, function (data) {

    });
});

}

cshtml:

<table class="table table-striped table-hover margin-top-20 pref-table" id="pref-table" [email protected]>
    @for (int i = 0; i < Model.BiData.Count; i++)
    {
        <tr [email protected][i].Name name=@i title="@Model.BiData[i].Name" class="tableRow">
            @Html.Hidden("resourceList[" + i + "]", Model.BiData[i].Name)
            <th>
                @Html.CheckBox("checkBoxList[" + i + "]", Model.BiData[i].Selected, new { @class = "resourceCheckbox" })
            </th>
            <th>
                @Model.BiData[i].Name
            </th>
        </tr>
    }


</table>

4 Answers 4

1

If your checkbox has some id like box then you can check if the event originated from that checkbox and stop processing.

$('#pref-table').on('click',function (event) {

        if(event.target.id === 'box'){
           return;
        }

        var resourceName = $(this).attr('title');
        var categoryName = $('#pref-table').attr('name');
        var url = "/Home/GetSpecific";
        $.post(url, { categoryName: categoryName, resourceName: myClass }, function (data) {

    });

Here's a Pen to demonstrate the idea.

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

2 Comments

It would be ok, but the event is not on row, but on entire table, I fail to send a parameter related to this row (title).
Using "#pref-table tbody tr" it work just fine. Thanks!
1

Try event.stopPropagation():

$('#pref-table input[type="checkbox"]').click(function(e) {
    e.stopPropagation();
});

1 Comment

It works besides the first click, wchich in case it is on checkbox, it still calls controller method. I also have jquery to check if checkbox is clicked to disable some button on the page (not related to this issue) - could this be the problem?
0

Using eventPropagation in the example below:

Html

<table width="100%">
  <tr style="background:yellow">
    <td>
      <input type="checkbox" />
    </td>
  </tr>
</table>

javascript/jquery

$(document).ready(function() {
  $('table tr').click(function(e) {
    alert("row clicked");
  });

  $('input[type=checkbox]').click(function(e) {
    e.stopPropagation();
    alert("checkbox clicked")
  });
});

Jsfiddle demo

Comments

0

I think your problem is you don't want to activate your event code when user clicks on checkbox, irrespective of checkbox state.

 $('#pref-table tbody tr').click(function (event) {
      if($(event.target).is(":checkbox")) return;
      // your event code
});

1 Comment

I doesn't work, i think that this refers to table row, not to checkbox.

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.