Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.preventDefault() working in Chrome, not Firefox for a submit button

Tags:

jquery

I have the following submit button:

 <input type="submit" name="submit" value="Send" class="mybutton" />

When I use the following code

$(document).ready(function(){
        $("#submit").submit(function(){
                event.preventDefault();
                console.log('test');
        }
});

My page is refreshing in Firefox, but not Chrome. Can anyone point me in the right direction?

I would like to use the submit button for a jQuery .ajax call (not a regular form submit)

like image 798
redconservatory Avatar asked Jan 12 '12 16:01

redconservatory


People also ask

Does preventDefault stop form submission?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form.

What can I use instead of event preventDefault?

The following example demonstrates how invalid text input can be stopped from reaching the input field with preventDefault() . Nowadays, you should usually use native HTML form validation instead.

Why is the event preventDefault () command used?

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link.

What is the difference between event preventDefault and return false?

preventDefault() prevents the default browser behavior for a given element. stopPropagation() stops an event from bubbling or propagating up the DOM tree. Whereas, return false is a combination of both preventDefault() and stopPropagation() .


4 Answers

You need make sure you add event to the parameter list of the event function. I think some browsers have a global event, that's why it works in some browsers.

$(document).ready(function(){
   $("#submit").submit(function(event){  // The event is passed to this function
      event.preventDefault();
      console.log('test');
   }
});
like image 111
Rocket Hazmat Avatar answered Oct 14 '22 10:10

Rocket Hazmat


Have you tried passing the event through as a variable?

$(document).ready(function(){
    $("#submit").submit(function(e){
            e.preventDefault();
            console.log('test');
    }
 });
like image 31
dyelawn Avatar answered Oct 14 '22 11:10

dyelawn


Simply in the function onclick for the submit button write down like this

onclick="return myfunction();"

and inside the function statements

myfunction () {
return false;};
like image 1
Abdulaziz Avatar answered Oct 14 '22 11:10

Abdulaziz


Pass the event parameter in the function callback Try this

$(document).ready(function(){
        $("#submit").submit(function(event){
                event.preventDefault();
                console.log('test');
        }
});
like image 3
ShankarSangoli Avatar answered Oct 14 '22 10:10

ShankarSangoli