-1

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

alench
  • 53
  • 6
  • Welcome to StackOverflow. You're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Please update your question to show what you have already tried in a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). For further information, please see [How to Ask](https://stackoverflow.com/questions/how-to-ask), and take the [tour](https://stackoverflow.com/tour) :) – Alive to die - Anant Mar 15 '23 at 08:28

1 Answers1

0

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.

Pouriversal
  • 66
  • 2
  • 6