Search code examples
jqueryeventskeyup

Filter document keyup event to specific element without selector


I'm dynamically creating elements that don't have a unique handle, so I can't really build a selector to pass to 'on' for filtering.

It also seems 'on' does not accept a jQuery element for the selector, according to the docs. I don't get why not, as the string selector has to be processed (incurring overhead) when I could directly provide the element in question.

Am I missing something obvious here?

$(document).on('keyup', elm, function(ev){ console.log( 'keyup on', ev.target ) });

Basically, I only want this instance of the event to fire if elm emits a keyup event.

EDIT

Forgot some context:

  • The element is not in the DOM yet, it is created in memory, and I'm trying to set up all the events there.
  • I tried binding the keyup event directly to the element, but it wouldn't fire. Not sure why. I assumed maybe because it wasn't in the DOM yet.
  • The element is not an input/textarea, I am relying on contenteditable/user-modify to allow input.

CONCLUSION

  • The issue was caused by my css. It seems that if the parent has user-modify, the child's keyup event won't fire. To be fair, user-modify should be targeted as possible to avoid DOM editing.

Solution

  • The issue was caused by my css. It seems that if the parent has user-modify, the child's keyup event won't fire. To be fair, user-modify should be as targeted as possible to avoid DOM editing.