Run jquery method on enter key

I try to run a method when I press the enter key. This method is already used by the button. Basically, when a user fills in a text field and presses the submit button, he gets a lightbox. I want to perform the same action, but when the enter key is pressed. Ive studied using .keypress, but I can only find examples where the form tag is a standard html form tag, and not an asp.net form tag. I think that at the moment the form only responds to .net controls on the page, and I cannot override this with the jquery method. Any ideas?

+4
source share
3 answers

You can associate a keypress event with a document and trigger() with a handler on your button.

 $(document).bind('keypress', function(e){ if(e.which === 13) { // return $('#buttonid').trigger('click'); } }); 

This will trigger every time you press the return key on your site. You probably want this to happen only if someone uses return in your input form. You just need to change the selector from document to any type of selector that matches your control.

Ref . : . trigger ()

+5
source
 $(document).keydown(function(e) { // test for the enter key if (e.keyCode == 13) { // Do your function myFunction(); } }); 
+1
source
 $(".input_loginform").keypress(function(e){ if (e.which == 13){ e.preventDefault(); alert("User pressed 'Enter' in a text input. sender form #"+this.form.id); postlogin("#"+this.form.id); } }); 
+1
source

All Articles