Enter event in text box

Normal java script

    function searchKeyPress(buttonName,e)
        var key;
              key = window.event.keyCode;     //IE
              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”>
         if (e.which == 13)
              $.get(‘text.ashx’, {
                       term: $(this).val()

                       //do something 
                        window.location = “”;

<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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: