Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  189] [ 2]  / answers: 1 / hits: 19984  / 12 Years ago, fri, august 10, 2012, 12:00:00

I've built a listener to prevent a form from submitting on a special ocasion.



I'm using google maps autocomplete api and basically I don't want to submit the form when the users press enter and the recommended results box is displayed. As the users are pressing enter to choose a value from the dropdown box and not to submit the form.



I've built a listener that catches the event correctly but I don't know how to prevent the form from being submitted.



$('body').live('keydown', function(e) {
if($(.pac-container).is(:visible) && event.keyCode == 13) {
e.preventDefault(); // Prevent form submission
}
});


I tried with e.preventDefault(); but it still submits the form. The form ID is: updateAccountForm



How can I prevent it from happening?



EDIT:
I must point out that it seems that listening for keypresses directly on the search input conflicts with Google API invalidating the autocomplete functions. So no $('input#search') keydown/keypress is possible


More From » forms

 Answers
136

I think you should try using something like this … wrapping in a check for the visibility of your container



$('body').keypress(function(e) 
{
if (e.keyCode == '13') {
e.stopPropagation()

}
});​


The article here has a good explanation.


[#83722] Wednesday, August 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
melinal

Total Points: 367
Total Questions: 101
Total Answers: 96

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
melinal questions
Thu, Dec 2, 21, 00:00, 3 Years ago
Tue, Jun 15, 21, 00:00, 3 Years ago
Tue, Feb 25, 20, 00:00, 4 Years ago
;