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)
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.
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.
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.
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() .
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');
}
});
Have you tried passing the event through as a variable?
$(document).ready(function(){
$("#submit").submit(function(e){
e.preventDefault();
console.log('test');
}
});
Simply in the function onclick for the submit button write down like this
onclick="return myfunction();"
and inside the function statements
myfunction () {
return false;};
Pass the event parameter in the function callback Try this
$(document).ready(function(){
$("#submit").submit(function(event){
event.preventDefault();
console.log('test');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With