...">

Can I associate a keyboard with an entire form?

Can I attach keyup()to the whole <form>? For example, I have several fields <input type="text">inside a form, and I want to associate an event keyupwith all of them, without attaching a keyup event handler to them. Rather, I want to associate a single event handler keyupwith all of them, so if any text field detects an event keyup, the event handler will be fired. Is this possible in jQuery?

I tried

$('form').keyup(function() {...});

But this made the keyup event handler unlimited after entering 1 character.

+4
source share
2 answers

, , keyup :

$('#myform input[type=text]').on('keyup', /* yada */);
+5

- :

$(function(){
   //create one instance for handler:
   var myHandler = function(e){ /* Your function */ };

   //Bind it:
   $("#form input[type=text]").on('keyup',function(e){ myHandler(e);  });

});
+2

Source: https://habr.com/ru/post/1532070/


All Articles