0

I have written code like this to prevent form submit. But it is not working. What is the possible reason

$('#signUpForm').submit(function (event) {
        debugger;
        console.log('test');
        event.preventDefault();
        $('#signUpForm').unbind().submit();
        //IsValid();
        return false;
    })
10
  • 2
    event.preventDefault(); is correct. Have you got a fiddle, or something showing exactly what is not working? Commented Apr 29, 2019 at 14:19
  • 2
    could you show your DOM code, you could be selecting the wrong Form element Commented Apr 29, 2019 at 14:20
  • 2
    This is jQuery not JavaScript Commented Apr 29, 2019 at 14:27
  • 2
    @Alexander Lallier I'm sure you'd agree that technically it's both really... jQuery is a JavaScript library and can't exist independently Commented Apr 29, 2019 at 14:28
  • 2
    you cannot say that jQuery is JavaScript - getting way off-topic now, but you can, and I do. Every time you use jQuery, you're using javascript, not just in a narrow "technical" sense but in reality. The same with any JS library or framework. Commented Apr 29, 2019 at 15:22

3 Answers 3

1

Try this solution:

$(document).ready(function() {
  $("#signUpForm").on("submit",function(e){
      e.preventDefault(e);
      console.log("form submission canceled");
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

this assumes there are no other forms on the page where this behaviour is not needed...we have no idea whether that's the case or not. If you're going to suggest a change like this, you should at least explain any possible unintended consequences, rather than just dumping the code. Also, there's already another answer suggesting the exact same thing...
1

Supposing you have only 1 form in your html page

$(document).ready(function() {
  $("form").submit(function(e){
      e.preventDefault(e);
      console.log("form submission canceled");
  });
});

1 Comment

this assumes there are no other forms on the page where this behaviour is not needed...we have no idea whether that's the case or not. If you're going to suggest a change like this, you should at least explain any possible unintended consequences, rather than just dumping the code.
-1

According to the jQuery documentation your syntax seems to be wrong.

Also, .unbind() is deprecated. Prefer .off()

try

 $('#signUpForm').unbind('submit')

4 Comments

You didn't explain what syntax exactly was wrong, and why. And then you state that unbind() is deprecated yet continue to provide a code example using it... Also we don't even know whether this is the problem OP is actually complaining about, since they haven't provided any proper details.
@ADyson I clearly explain why his syntax is wrong with the code provided
No you don't, you just state that it's wrong, without even explaining which bit is wrong, or why, or why your change is correct. You given an alternative, but you haven't explained anything
I also don't understand why you give an example using a method you already mentioned is deprecated

Your Answer

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.