mscommunity.net

Interactive mscommunity.net online activities
Signed in as anonymous | Edit Profile | Sign out | Help
in Search

Weblog :: Boris Ševo

Sporadic posts about my interests, e.g. software development (mostly .NET), technology in general and some occasional rant.

Using jQuery to prevent form submit when enter is pressed

I was recently working on a ASP.NET RIA which has a web form with the input field in which users can type some search term and press the search button or hit enter key. Search is invoked by an asynchronous call and done on the server. The text field is placed inside of form tag and because of that, when someone hits the enter key, instead of the desired behavior (asynchronous call), the whole form is submitted to the server. There is more then one possible solution for this kind of problem, but because I was already using jQuery for building the whole application's UI, I fixed the problem with the following elegant jQuery's cross-browser code snippet:

     $(document).ready(function() {
         $("form").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                
search($("#searchTerm").attr('value'));
                 return false;
            }
         });

     });
Published pro 11 2008, 11:51 by boris.sevo
Filed under:

Comments

 

anonymous said:

Great! Thanks!

veljača 4, 2009 3:45
 

anonymous said:

Thank you!!

svibanj 29, 2009 9:20
 

anonymous said:

thanks

srpanj 26, 2009 11:58
 

anonymous said:

Thanks!

It worked like a charm =)

kolovoz 6, 2009 7:01
 

anonymous said:

$("#name").keyup(function(event){

 if(event.keyCode == 13) {

     alert('you pressed enter');

  }

});

listopad 13, 2009 4:07
 

anonymous said:

Thank you that helped

studeni 3, 2009 3:28

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Commercial Edition), by Telligent Systems