Josh Randall
Josh Randall

Reputation: 1344

Problem preventing form submit using jquery

I have simple form. I want to use jquery to prevent form submit.

$(document).ready(function() {
    $('#searchform').submit(function(e) {
         alert('Handler for .submit() called.');
         event.preventDefault();
         return false;
   });
});

This works fine in chrome. It just shows me a alert box and does not redirect. But does not work on Firefox or IE9, it shows the alert and then goes ahead with form submission.

I appreciate any help.

Upvotes: 0

Views: 73

Answers (3)

Matt Ball
Matt Ball

Reputation: 359816

Since you're already returning false, you don't also need any sort of event.preventDefault() call at all.

The reason that neither of these seem to work for you is that event is undefined, so an exception is thrown when calling event.preventDefault(), so the method handler exits prematurely.

Upvotes: 1

mkk
mkk

Reputation: 7693

change

 event.preventDefault();

into

 e.preventDefault();

Upvotes: 2

Kyle
Kyle

Reputation: 4449

event.preventDefault();

should be

e.preventDefault(); 

Try that and see if it fixes the problem.

Upvotes: 0

Related Questions