Oz Bar-Shalom Oz Bar-Shalom - 1 month ago 15
HTML Question

jQuery - element.on click for match cases

I have an element that is inside a table somewhere.
I need to get his tbody element.

How can I do that?

Tried

$(this).closest('tbody')


But it looks like it did not work..

My code:

$("table").on('click', ['td'], function(event) {

if ($(this).hasClass('uneditable')) { return; }
// here I need to check the tbody element
console.log($(this).closest("tbody"))
}


Maybe I am doing this wrong, because I want to apply this function only if a click on a table with specific classes for elements:


  • table -
    dattable

  • tbody -
    editabe

  • td - not
    uneditable



Thanks !

Answer

So just bind click to the tbody that has the class and check the class on click.

$("table.dattable tbody.editable").on("click", "td:not(.uneditable)", function(){});

Or id the .editable is dynamic on the table than move it to the other selector

$("table.dattable").on("click", "tbody.editable td:not(.uneditable)", function(){});
Comments