I am trying to get the Enter key to trigger a function when it is pressed when inside a certain textbox, and not trigger the first or default button.
You can see an
$(document).ready(function() {
$('#myText').keypress(function(e) {
if ( e.keyCode == 13 ) { // detect the enter key
$('#myButton').click(); // fire a sample click, you can do anything
}
});
$('#myButton').click(function(e) {
alert('Button click activated!');
});
});
DEMO
For live elements use .on() like below:
$(document).ready(function() {
$(document).on('keypress', '#myText', function(e) {
if ( e.keyCode == 13 ) { // detect the enter key
$('#myButton').click(); // fire a sample click, you can do anything
}
});
$(document).on('click', '#myButton', function(e) {
alert('Button click activated!');
});
});