user987723
user987723

Reputation: 965

onkeypress event not firing in ie

$(".tbSearchBox").keyup(function (event) {
    if (event.keyCode == 13) {
        alert("ye");
        searchSet = $(this).val();
        $(".btnSerachButton").click();
    }
});

Im using the above code to detect whether the user has typed something in a search box then hit enter instead of pressing the search button. this works for all browsers apart from IE. IE can read the on keyup event but bypasses the if statement used. Any idea why?

Upvotes: 0

Views: 3440

Answers (3)

dku.rajkumar
dku.rajkumar

Reputation: 18568

There some incompatibility in ie regarding event and keycode so to make it browser compatible try this

$(".tbSearchBox").keypress(function (event) {
    var ev = event || window.event;
    var key = ev.keyCode || ev.which || ev.charCode;
    if (key == 13) {
        ev.preventDefault();
        alert("ye");
        searchSet = $(this).val();
        $(".btnSerachButton").click();
    }
});

Upvotes: 1

Johan
Johan

Reputation: 35194

var code = (event.keyCode ? event.keyCode : event.which);

or maybe even

var code = event.keyCode || event.which;

Upvotes: 1

Rich O'Kelly
Rich O'Kelly

Reputation: 41757

You should use event.which here to normalise event.keyCode and event.charCode:

if (event.which == 13) ...

Upvotes: 0

Related Questions