Keyboard enter button on input

203 Views Asked by At

Anyone can teach me how to make input button onlick when press the keyboard "Enter" ?

What i have study from web is something like this , but it's does't work when i press enter from keyboard . Anyone can show me how ??

<input type="text" id="info" name="info" />
<input type="button" id="bn" name="bn" value="click" onclick="read()" />

<script>

$(document).ready(function(){
    $('#info').keypress(function(e){
      if(e.keyCode==13)
      $('#bn').click();
    });
});

function read()
{
// do smth
}
</script>
2

There are 2 best solutions below

0
On BEST ANSWER

I am adding fiddle : http://jsfiddle.net/nzu27rw4/1/ make sure that you have added jquery reference on the top of this script

$(document).ready(function(){
    $('#info').keypress(function(e){
      if(e.keyCode==13)
      $('#bn').click();
    });
});

function read()
{
alert('read')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="info" name="info" />
<input type="button" id="bn" name="bn" value="click" onclick="read()" />

0
On

Try this code:

    <input type="text" id="info" name="info" />
    <input type="button" id="bn" name="bn" value="click" onclick="read()" />

    <script>

    $(document).ready(function(){
    $('#info').keyup(function(e){
      if(e.keyCode==13)
      $('#bn').trigger("click");
    });
});

function read()
{
alert('clicked')
}
    </script>