Enter event in text box

Normal java script

    function searchKeyPress(buttonName,e)
    {
        var key;
           if(window.event)
              key = window.event.keyCode;     //IE
        else
              key = e.which;     //firefox   
           if (key == 13)
          {
              // do something
                    var btn = document.getElementById(buttonName);
          }
   }

<input id=”txtSearch” onkeypress=”searchKeyPress(search, event);” type=”text” />

Using JQuery

<script type=”text/javascript”>
$(function()
{
    $(‘#search’).keypress(function(e)
    {
         if (e.which == 13)
         {
   
              $.get(‘text.ashx’, {
                       term: $(this).val()
                   },
                   function(data){

                       //do something 
                        window.location = “http://192.168.2.177/search/result.aspx?tt=0&q=”;
                   }
              );
         }
    });
}); 
</script>

<input type=”text” id=”search”/>

NOTE: this only works well if there only one button in the web page or other buttons are default ones for other parts. If not, the other buttons may run before our function. In some cases, it does not work with textbox server control, so it is better if we use html control instead.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: