3

I have a form which I want to submit only if a condition is true. But there is also a case in which I want to programmatically.

Following works when manually submitting the form:

<input type="submit" value="submit" onclick="return checkForTheCondition();"/> 

Following works for the submitting the form programmatically but it doesn't check for the condition for submitting the form.

$('form').submit(); 

So I tried to do the following for programmatically checking the condition before submitting the form but when I use the following code it doesn't even submit the form which was being done by $('form').submit();:

$('form').submit(function() { return checkForTheCondition(); }); 

checkForTheCondition() returns either true or false.

Is it possible to programmatically submit the form just like it works for manual form submission where it checks for a condition before submitting the form?

4 Answers 4

4

You should be able to do it this way:

if (checkForTheCondition()) { $('form').submit(); } 

Edit: I think I understand your problem now.

Calling $('form').submit(function () { ... }); does NOT trigger the event. The function is not a callback! Instead it binds the function to the submit event. So when you call this:

$('form').submit(function() { return checkForTheCondition(); }); 

You do not trigger the event, insted you bind return checkForTheCondition(); this is somewhat the same as onclick="return checkForTheCondition();" but it will be called everytime the form is submitted, and not only when the submit button is clicked.

I would do it this way:

<input type="submit" value="submit"> <script> $(document).ready(function () { $('form').submit(function() { return checkForTheCondition(); }); }); </script> 

Then you can just call $('form').submit(); and checkForTheCondition() will be called each time. You can read more about jQuery and submit here. Good luck

Edit again:

I realise the easiest solution would be this:

<form method="post" action="somepage" onsubmit="return checkForTheCondition();"> <!-- Inputs --> <input type="submit" value="submit"> </form> 

This will make $('form').submit(); and clicks on the submit button call checkForTheCondition() before submitting. Enjoy!

Sign up to request clarification or add additional context in comments.

3 Comments

No he can't. There is a submit button that just submit, It doesn't pass through the checkForTheCondition on the way.
I believe he wants to triger the submit event from code, and want the checkForTheCondition() tested. This code will do the validation and raise the event.
@JPuge: It couldn't get cleaner than this and fortunatly it solves the problem as well and I'll accept the answer as well but I am not able to understand that why the execution doesn't get into the callback function? There is something still very surprising that I see, my breakpoints dont work the first time the code runs but when I come back again to the page the let the code run programmatically again the breakpoints work as well. The triggerSubmit() is being called from a setTimeout(triggerSubmit, timeLeft). Does it have to go anything with the setTimeout()?
1

Your code should work if it returns true/false conditionally. You can try this if you want to alert any message on failure of condition.

$(function(){ $('form').submit(function() { if(!checkForTheCondition()){ //alert message here return false;//This will stop the form from being submitted. } return true; }); }); 

3 Comments

Yes, it is but here OP can alert a message if he wants to and more over it is easy to understand what actually is happening looking at the code.
@ShankarSangoli: It isn't getting into the callback function. I've checked the logs. But when I click the form submit button I do get to see the the result of the checkForTheCondition() in true or false in the logs.
May be the form is not available on the page when that code is executed with attached submit event handler. I moved it to dom ready event now, try my edited answer.
0
$('#my_form_id').submit(function() { var my_value = checkForTheCondition(); if(my_value == "some returned value"){ return true; } else { alert("error in your submission"); return false; } }); 

5 Comments

I would recommend binding more closely to the submit event. Forms can get submitted without clicking the submit button.
@jpea: I've tried the your code, checked the log but for some reasons it doesn't get into the callback function. but then I click the submit button manually it submits the form after checking the condition. Am not able to understand why is it not getting into the callback function?
@cheeken: I am going to try that now as the callback function don't seem to work. I barely know any javascript.
oops, edited to fix the callback function - I forgot the equals sign after the my_value declaration.
@jpea: The execution is not getting into the callback function and I am not quite able to understand why. Jpuge's answer has worked though.
0
if ($( "#formID" ).submit()){ alert( "Handler for .submit() called." ); }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.