Search code examples
jquerysetfocus

Setting focus on a button is not working


I am trying to set the focus to a button while the user presses the Enter key in the text box. But it is not working. I am using the Internet Explorer 8 browser. Am I missing something?

$("input.Box").live('keydown', function(e) {
    if (e.keyCode == 13) {
        e.preventDefault(); 
        $("#button").focus(); // Not working?
    }
});

Solution

  • Microsoft decided that they don't like e.keyCode and instead have their own syntax, e.which.

    You have to check for both:

    $("input.Box").live('keydown', function(e) {
        var keyCode = (window.event) ? e.which : e.keyCode;
    
        if (keyCode == 13)
            e.preventDefault(); 
            $("#button").focus(); // Not working?
        }
    });