J3RN
J3RN

Reputation: 1843

Prevent form from submitting with jQuery

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?

Upvotes: 0

Views: 81

Answers (3)

Sushanth --
Sushanth --

Reputation: 55740

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();
    });
});

Upvotes: 0

adeneo
adeneo

Reputation: 318182

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

Upvotes: 4

KaeruCT
KaeruCT

Reputation: 1645

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

Upvotes: 0

Related Questions