0

So this is driving me nuts. This code,

$('#the_form').submit(function(e){
            alert("Submit!");

            return false;
            e.preventDefault();
        });

should prevent my HTML form,

<form id="the_form">
                <input type="text" name="q" />
                <input type="submit" />
            </form>

from refreshing the page, but it doesn't. Does anyone have insight on this?

2
  • 3
    No code is executed after return statement. Commented Oct 24, 2012 at 0:51
  • jsfiddle.net/oceog/G9JLG/1 it works as is, but yes, no preventDefault() needed. Commented Oct 24, 2012 at 0:55

3 Answers 3

4
$(function() {
    $('#the_form').on('submit', function(e){
        e.preventDefault();
        alert("Submit!");
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

I was thinking about document ready handler. this should be the issue.
@undefined i think you right, because return false, prevents form from submitting
@eicto Yes return false === event.preventDefault() + event.stopPropagation();
Both the return false and the preventDefault should stop the form, only reason I could think of why it would'nt, would be a missing DOM ready function? On the other hand one does'nt really need both, and there's no good reason to stop the propagation of a form submit that has been prevented.
0

Try removing return false from your code, or putting it after e.preventDefault().

Comments

0

Just remove the return statement..

Also Make sure your code is encased in DOM ready Handler

$(document).ready( function() {
    $('#the_form').submit(function(e){
         alert("Submit!");
         e.preventDefault();
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.