0

I simplified my code to reduce extraneous information, but I am having the same problem with the reduced code as with the more complex version. I have a form like this:

<form id="password_reset_form" action="script_reset_password.php" method="POST">
 <input type="submit" value="Set Password" class="button-inactive" />
</form>

and jQuery code like this:

<script type="text/javascript">
 $(document).ready(function(){
 $("#password_reset_form").submit(function(){
 alert("form submit attempted");
 event.preventDefault();
 }
 });
</script>

But the jQuery .submit() is not being called. The goal is to prevent the form submission and display an error message if a particular condition is met, but my question is, why isn't .submit() being called when the <input type="submit" /> is clicked?

asked Oct 19, 2015 at 18:59
2
  • 1
    Your submit function code is missing a closing ) Commented Oct 19, 2015 at 19:03
  • Change } to }); -- you must be seeing a syntax error. Dev tools are your friend, don't give them a cold shoulder. :-) Commented Oct 19, 2015 at 19:06

2 Answers 2

5

You need to catch the event from within the callback function.

$(document).ready(function(){
 $("#password_reset_form").submit(function(e){
 alert("form submit attempted");
 e.preventDefault();
 });
});
answered Oct 19, 2015 at 19:02
Sign up to request clarification or add additional context in comments.

4 Comments

And close the event binding with a );.
Works fine without it for me (even though I agree that it belongs there). jsfiddle.net/j08691/mpbtd4cz
@j08691 nice to know that event is defined in the scope even if it wasn't defined...
@ DontVoteMeDown - Yeah but not in all browsers AFAIK for better support you must pass it in to the callback.
2

You have syntax errors in your code, The document ready needs to be closed.

$(document).ready(function(){
 $("#password_reset_form").submit(function(evt){
 alert("form submit attempted");
 evt.preventDefault();
 });
});

https://jsfiddle.net/h19bk8ft/2/

answered Oct 19, 2015 at 19:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.