How do I cancel form submission in submit button onclick event?
You are better off doing...
<form onsubmit="return isValidForm()" />
If isValidForm()
returns false
, then your form doesn't submit.
You should also probably move your event handler from inline.
document.getElementById('my-form').onsubmit = function() {
return isValidForm();
};
javascript stop button onClick from submitting form
The default type of a button is "submit"
; just override that behavior by setting it to "button"
.
cell2.innerHTML = "<button type='button' class='remove-condition' onclick='removeCondition()'>X</button></td>";
You also need to define event
as a parameter of the event handler function.
function removeCondition(event) {
// event.target will be the input element.
var td = event.target.parentNode;
var tr = td.parentNode; // the row to be removed
tr.parentNode.removeChild(tr);
};
JavaScript code to stop form submission
You can use the return value of the function to prevent the form submission
<form name="myForm" onsubmit="return validateMyForm();">
and function like
<script type="text/javascript">
function validateMyForm()
{
if(check if your conditions are not satisfying)
{
alert("validation failed false");
returnToPreviousPage();
return false;
}
alert("validations passed");
return true;
}
</script>
In case of Chrome 27.0.1453.116 m if above code does not work, please set the event handler's parameter's returnValue field to false to get it to work.
Thanks Sam for sharing information.
EDIT :
Thanks to Vikram for his workaround for if validateMyForm() returns false:
<form onsubmit="event.preventDefault(); validateMyForm();">
where validateMyForm() is a function that returns false if validation fails. The key point is to use the name event. We cannot use for e.g. e.preventDefault()
Prevent submit button with onclick event from submitting
You'll need to add the event as a parameter:
$j('form#userForm .button').click(function(event) { // <- goes here !
if ( parseInt($j("#zip_field").val(), 10) > 1000){
event.preventDefault();
$j('form#userForm .button').attr('onclick','').unbind('click');
alert('Sorry we leveren alleen inomstreken hijen!');
}
});
Also, val()
always returns a string, so a good practice would be to convert it to a number before you compare it to a number, and I'm not sure if you're really targeting all .button
elements inside #userForm
inside the function, or if you should use this
instead?
If you're using jQuery 1.7+, you should really consider using on()
and off()
for this.
React - Preventing Form Submission
I think it's first worth noting that without javascript (plain html), the form
element submits when clicking either the <input type="submit" value="submit form">
or <button>submits form too</button>
. In javascript you can prevent that by using an event handler and calling e.preventDefault()
on button click, or form submit. e
is the event object passed into the event handler. With react, the two relevant event handlers are available via the form as onSubmit
, and the other on the button via onClick
.
Example: http://jsbin.com/vowuley/edit?html,js,console,output
Disable form auto submit on button click
Buttons like <button>Click to do something</button>
are submit buttons.
Set type="button"
to change that. type="submit"
is the default (as specified by the HTML spec):
The missing value default and invalid value default are the Submit Button state.
Related Topics
JavaScript String/Integer Comparisons
Jquery: Get Height of Hidden Element in Jquery
How to Show the "Are You Sure You Want to Navigate Away from This Page" When Changes Committed
Custom Attributes - Yea or Nay
Trigger Event When User Scroll to Specific Element - With Jquery
Best Way to Track Onchange As-You-Type in Input Type="Text"
How to Run a Function When the Page Is Loaded
Onclick to Get the Id of the Clicked Button
Load and Execution Sequence of a Web Page
Using an HTML Button to Call a JavaScript Function
Jquery Disable/Enable Submit Button
How to Implement "Select All" Check Box in Html
Determine If an HTML Element'S Content Overflows
Converting HTML String into Dom Elements
JavaScript Confirm Popup Yes, No Button Instead of Ok and Cancel