Manish
Manish

Reputation: 1326

Capture enter event and redirect using jquery

I have a functionality on home page of my site where user enters dish name and presses enter, enter event should be captured and it should redirect to listings page along with dish name as querystring.

I'm using below code, which works only second time when I click enter.

 $(document).ready(function () {
            $(document).keypress(function (e) {
                if (e.which == 13) {
                    // enter pressed
                    var searchKeyWord = $("#SearchTextBox").val();
                    window.location.href = "/Listings.aspx?K=" + searchKeyWord;
                }
            });
        }); 

you can see the functionality at http://khanawal.com/home.aspx Any help would be appreciated.

Upvotes: 3

Views: 1800

Answers (1)

Brigand
Brigand

Reputation: 86250

It appears to work as long as the search box isn't selected. Try binding to both.

$(document).ready(function () {
        function handleEnter(e) {
            if (e.which == 13) {
                // enter pressed
                var searchKeyWord = $("#SearchTextBox").val();
                window.location.href = "/Listings.aspx?K=" + searchKeyWord;
            }
        }

        $(document).keypress(handleEnter);
        $("#SearchTextBox").keypress(handleEnter);

}); 

If that doesn't fix it, there's probably a problem elsewhere in your code. Search for stopPropagation in your code.

Upvotes: 1

Related Questions