Bill
Bill

Reputation: 1477

Combine multiple jQuery events based upon ID's

I have 8 tables on a page and four of the have sortable events wired to then such as:

       $("#positionTable tbody").sortable({
          ...do stuff
         }).disableSelection();

and for visual

           $("#positionTable tbody").on("mousedown", "tr", function () {
           if ($('#positionTable').text() == "Done") {
              $(this).css('backgroundColor', '#e1e1e1');
          }
        });

and two others. How can I combine the events into one and then read the one firing the event? I've tried

      $("#positionTable tbody, #degreeTable tbody").sortable({ 

and other combinations but it does not seem to work. I would love to take these 16 functions and combine them into 4. I've seen here how to combine for button click with classes but nothing in this area. I may have missed it and if I did I'm sorry.

Thanks in advance.

Upvotes: 0

Views: 26

Answers (1)

Neil
Neil

Reputation: 14313

You can do this by using relative calls like $(this).parent(). So, regardless of which tbody you select, it will then automatically find the right table and then check that.

$(function () {
    $("#positionTable tbody, #degreeTable tbody").sortable().disableSelection();;

    $("#positionTable tbody, #degreeTable tbody").on("mousedown", "tr", function () {
        if ($(this).parent().text() == "Done") {
            $(this).css('backgroundColor', '#e1e1e1');
        }
    });
});

Upvotes: 1

Related Questions