Capture Enter in IE using jQuery

Asked

Viewed 498 times

4

Guys I have the following code in jQuery:

    $(document).keypress(function(e) {
        if(e.which === 13) $('.button_ok').click();
    });

When I click the enter key, it causes the user to have clicked the class button button_ok.

It’s working on all browsers except Internet Explorer. Does anyone know what it might be?

  • This question has two votes to close as not clear enough. To me, it seemed to be quite clear, so I voted to keep it open. Someone who has voted or will vote for closure, I would like to speak out to explain what is wrong?

  • When checking the IE Console tab (F12), does the browser report an error after you type something? If yes, post your question together.

  • I believe it is necessary to complement the post by saying which version of jQuery is being used.

5 answers

4


Second that reply found in the So-En e.which does not work in the IE.

An alternative is you use e.KeyCode and use keydown instead of keypress.

Press run and you will see that Alert will show you the message: the enter key has been pressed.

document.addEventListener('keydown', function(e){
       if(e.keyCode == 13){
          alert('a tecla enter foi pressionada');
       }
    }, false);

  • 1

    yours worked 100%, as it turns out in jQuery?

  • 1

    @Hugoborges follows Fiddle

  • vlw itself :)

2

The problem that occurs in your code is that the function which not supported in older versions of Internet Explorer, only from version 9 of it.

So to solve your problem, with a simple solution and cross browser capture of the enter key, you can do as follows below:

$(document).keypress(function(e){
    var keycode = e.keyCode ? e.keyCode : e.which;
    if(keycode === 13){
        $('.button_ok').click();   
    }
});

$(document).keypress(function(e){
  var keycode = e.keyCode ? e.keyCode : e.which;
  if(keycode === 13){
    $('.button_ok').click();   
  }
});

$('.button_ok').click(function() {
  alert('OK!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" name="txt_input" />
<input type="button" class="button_ok" value="OK" />

    $(window).keypress(function(e){
      var keycode = e.keyCode ? e.keyCode : e.which;
      if(keycode === 13){
        $('.button_ok').click();   
      }
    });

    $('.button_ok').click(function() {
      alert('OK!');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="button" class="button_ok" value="OK" />

  • I tried here and it didn’t work out

  • Is there no mistake?

  • I put an example in the post, check if it doesn’t help.

  • removes the field txt_input and it stops working

  • What version of Internet Explorer are you using?

  • is version 11.545.10586.0

Show 1 more comment

2

<input type="text" value="texto do input" id="input">
<input type="button" value="click" id="click">

$('#click').on('click', function () {                    
 alert($('#input').val());
 });
 $('#input').on('keydown', function (e) {                     
     if (e.keyCode === 13) {
         $('#click').trigger('click');
     }
 });

1

I use it a lot with internet explorer compatibility:

$(function() {
    $(window).on('keydown', function(event) {
        if(event.which == 13) {
            suaAcao();
            return false;
        }
    });
});

0

Use the e.keyCode:

$(document).keypress(function(e) {
     var code = e.keyCode || e.which; 
     if(code === 13) $('.button_ok').click();
});
  • I tried here and it didn’t work out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.