After changing my button type
to submit
it’s not submitting the form. Somehow, the AJAX request is not working after that. If I change it to type="button"
then it’s working, but I want this because required validation is not working while giving type="button"
. It only works when I’m giving button type submit but then the form is not submitting.
<form>
<input type="email" class="form-control" id="passwordReset" placeholder="Email" required/>
<button type="submit" id="passwordButton"> Submit</button>
</form>
$("#passwordButton").on('submit', function(e) {
e.preventDefault();
const email = $("#passwordReset").val();
$.ajax({
type: "GET",
url: "/forget_password=" + email,
success: function(response) {
if (!response.data) {
$(".sendError").show();
} else {
$(".sendSuccess").show();
}
}
});
})
I want my form to be submitted while checking the required
condition also.
4
Answers
Quoting from MDN:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event
Attach the listener to the
click
even of the button, or to thesubmit
event of the form.HTMLFormElement: submit event
Try
click
event instead:Update: I think simply submit event on the form is enough here as the event will fire on clicking any input
type=submit
:I think you should just create a normal like
<button onclick="Submit()">save</button>
and write Code like that:
Add Id or class attribute to your form.
Then,
Things to remember: