Dojo: preventing form submit on Enter
If you've got some AJAX functionality inside a standard form (e.g. an address lookup feature where people type in their postcode and click a button) then there's a temptation for people to hit 'Enter' to perform the search. By default this will submit the whole form, which isn't what you want.
Catching the 'Enter' key press is fairly easy to do with some inline Javascript, but it can easily be done unobtrusively with Dojo as well:
dojo.addOnLoad(
function() {
dojo.connect(dojo.byId('postcode'), 'onkeydown', function(event){
if (event.keyCode == dojo.keys.ENTER) {
// CALL AJAX CODE HERE
dojo.stopEvent(event);
}
});
}
);
I found that 'onkeyup' as the event doesn't work here - the code runs but doesn't prevent the submission, so stick to onkeydown.
Comments
22:10, on 21/4/2011
11:24, on 26/4/2011
Add Comment