1

I tried this:

$('body').on('click', '.headerCheckbox', function (event) { // Add event here and magic should happen?
    event.stopPropagation(); // event output in firebug: event: [object Object]
    headerCheckboxClicked($(this));
});

It's a table, where I have a checkbox in a header in one of the columns. I want to stop event bubbling so the table doesn't get the event so it doesn't sorts the column.

I thought this was the right way but the table stills sorts after I added the line. I can't find a good example to work with in the jQuery Documentation.

Alternative solution The datatable way I disable sorting on the specify column.

    table3 = $("#tableComputerPackages").DataTable({
        "order": [[1, "desc"]],
        "columnDefs":
        [
            {
                "targets": 0,
                "orderable": false
            }
        ]
    });

A. Wolff had the answer to the question thought. It was because I wrote the on() with the 3 parameters version instead of 2 which I am use to, so I didn't even think about it.

6
  • 1
    And how is the event attached to the table? Commented Mar 16, 2015 at 13:49
  • The table is from datatable.net. They have their own events. I didn't do anything with that, if that's what you mean. I thought I just could add event to function(event).. Commented Mar 16, 2015 at 13:52
  • 1
    This is due to how delegation works. Here, your click event already has bubbled through the DOM to the body level. Simplest solution is to attach onclick event using inline attribute, see e.g, stackoverflow.com/a/28571944/1414562 Now, i'm not a datatable expert but i guess you could filter it using more relevant method and avoid sorting if the target event is a checkbox. Maybe provide more info as how do you initialize datatable in question itself. Anyway, adding datatable tag in question, so an expert could more easily see it and maybe answers. Commented Mar 16, 2015 at 14:16
  • Ahh ofcourse. It's because the bubbling already reached up to the body. Makes sense. Commented Mar 16, 2015 at 14:18
  • 1
    That's said, if your datatable headers are static, you should just be able to bind event directly to checkboxes: $('.headerCheckbox').on('click', function (event) {event.stopPropagation(); //...}); Commented Mar 16, 2015 at 14:18

1 Answer 1

2

If your datatable headers are static, you should just be able to bind event directly to checkboxes:

$('.headerCheckbox').on('click', function (event) {
      event.stopPropagation(); 
      headerCheckboxClicked($(this));
});

Your issue was regarding how delegation works, using event bubbling (and filtering it out using event target), so actually you were stopping propagation at body level, the header click event was already fired, and so the sorting method already called.

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

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.