Limit keydown execution only to specific div

36 Views Asked by At

I want to limit execution with hit enter only to specific div, not on whole page so that this enter key not impact to divs outside this #targer-wrapper but code below with checking div length didn't works:

if ($('#targer-wrapper').length) {

    $('#targer-wrapper').on('keydown', '#children-input', function (e) {

        value = $('input#children-input').val();

        if (value.length >= 3) {
            functionName();
        } else if (e.which === 13 || e.keyCode === 13) {
            functionName();
        }

    });
}

Thanks for help and suggestion

1

There are 1 best solutions below

1
Pourya Tak On

As you want to fire a Function on a specific element, you should select that element ( in your case, #targer-wrapper ) and set the eventListener on it:

if ($('#target-wrapper').length) {
  $('#target-wrapper').on('keydown', function(event) {
    // Your event handling code here
  });
}

You should use a direct eventListener rather than relying on event propagation.