Search code examples
jqueryselectinputonchangeonkeyup

Can we combine two function in jquery with different event type


Is there anyway i can combine this :

$("tbody").on("keyup", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4)",
function () {
    $(this) //input
    .closest("tr") //finds the row associated with the input
    .find("td:first input") //returns the first cell
    .val(this.value); //sets the text
});

$('select').on('change', function() {
    $(this).closest("tr").find("td:first input").val( this.value );
});

to only one function for select change and keyup for input like:

$("tbody").on("input keyup select change", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4)",
...
});

also i want to set it like the first input of type=text like :

$(this).closest("tr").find("td:first input[type="text"]").val( this.value );

but its not working.

fiddle here


Solution

  • Try this:

    $("tbody").on("keyup change", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4) select",
    function () {
        $(this) //input
        .closest("tr") //finds the row associated with the input
        .find("td:first input") //returns the first cell
        .val(this.value); //sets the text
    });
    

    DEMO