slandau
slandau

Reputation: 24102

Binding to key press doesn't work in IE8 and older

I have this code on my web page:

$(document).keypress(function(e){
    if (e.which == 13){
        if ($('#title').is(":focus"))
        {
            $("#save_post").focus().click();
            $('div .jqEasyCounterMsg').css('visibility','hidden');
        }
        else if ($('#s').is(":focus"))
        {
            $("#searchAddress").focus().click();
        }
    }
});

This works in every single browser except older versions of IE (8 and older). What should I change to get this to work in those versions?

Upvotes: 1

Views: 1751

Answers (1)

karim79
karim79

Reputation: 342795

Place your event handler code within a $(document).ready(... block and it should work.

$(document).ready(function() {
    $(this).keypress(function() {
        ...
    });
});

See http://api.jquery.com/ready/

Upvotes: 2

Related Questions