oshirowanen
oshirowanen

Reputation: 15925

Execute function when enter is pressed if textbox has focus

Is it possible to execute a function by pressing the enter key on the keyboard only if a certain textbox has focus. i.e. if any other textbox has focus or no textbox has focus, the enter key should do nothing.

Upvotes: 13

Views: 13622

Answers (3)

Code Spy
Code Spy

Reputation: 9954

Enter any thing in Text box then press Enter to execute

 <form onSubmit='alerttest(this); return false'>        
     <input type="text">         
 </form>


 <script language="javascript">
   function alerttest()
    {
        alert("Executing jolly.exe !!!!");
        // function edited by Inderpreet singh
    }
</script>

Upvotes: -2

genesis
genesis

Reputation: 50966

$(function(){
     $("#textarea").keyup(function(e){
          if (e.keyCode === 13) {
               //do stuff
          }
     });

});

Upvotes: 5

Seth
Seth

Reputation: 6260

$('#myTextbox').bind('keyup', function(e) {

    if ( e.keyCode === 13 ) { // 13 is enter key

        // Execute code here.

    }

});

Upvotes: 32

Related Questions