Search code examples
javascriptjqueryajaxeventspreventdefault

event.preventDefault not working


I have this simple code here, nothing too advanced.

    $("input#send").submit(function(event){
      event.preventDefault();
    $.ajax({
        type: 'POST',
        url: add.php,
        data: data,
        success: success,
        dataType: dataType
    });
});

Whever I click on the "send" button, the event.preventDefault function doesn't work, and the page loads.

Any ideas why?


Solution

  • A form has the submit event, not a button. Plus, an ID should be unique so tag#id can just be #id.

    $("#theform").submit(function(event) {
        event.preventDefault();
        // ...
    });