Forum Moderators: open
// bind the enter key to the login dialog submit button.
$('#login-window').keypress(function(event) {
if( event.keyCode == $.ui.keyCode.ENTER ) {
$(this).parent().find('.ui-dialog-buttonpane button:first').click();
return false;
}
});
// bind the enter key to the workorder dialog submit button.
$('.ui-dialog').keypress(function(event) {
if( event.keyCode == $.ui.keyCode.ENTER ) {
$(this).find('.ui-dialog-buttonpane button:first').click();
return false;
}
});
<div class="ui-dialog">
<div id="login-window" class="ui-dialog-content">
<div class="ui-dialog-buttonpane">
<div class="ui-dialog-buttonset">
<button class="ui-button">
<button class="ui-button">
</div>
</div>
</div>
</div>
$(this).find('.ui-dialog-buttonpane button:first').click();
$('.ui-dialog-buttonpane button:first', $(this)).click();
$('.ui-dialog').live('keyup', function(event) {
if (event.keyCode === $.ui.keyCode.ENTER) {
$('.ui-dialog-buttonpane button:first', $(this)).click();
}
});It uses the ".live" event which has been deprecated. $('.ui-dialog').on('keyup', function(event) {
if (event.keyCode === $.ui.keyCode.ENTER) {
$('.ui-dialog-buttonpane button:first', $(this)).click();
}
});But it does not work. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.
$('body').on('keyup', '.ui-dialog', function(event) {
if (event.keyCode === $.ui.keyCode.ENTER) {
$('.ui-dialog-buttonpane button:first', $(this)).click();
}
});It works! $(this).find('.ui-dialog-buttonpane button:first').click();because the syntax says find within "this" the first button within all ui-dialog-buttonpane classes.
$('.ui-dialog-buttonpane button:first', $(this)).click();because it's finding the first button within all ui-dialog-buttonpane classes and also find all "this" - which doesn't make too much sense but it works.
$('body').on('keyup', '.ui-dialog', function(event) {
if (event.keyCode === $.ui.keyCode.ENTER) {
$(this).find('.ui-dialog-buttonpane button:first').click();
}
});