How to get the Enter key within a textbox to trigger a function and not the first/default button

Try this:

$('#myText').on("keypress", function(e) {
        if (e.keyCode == 13) {
            alert("Enter pressed");
            return false; // prevent the button click from happening
        }
});

Demo

Leave a Comment