JakePump
JakePump

Reputation: 7

Switching a var from true to false

Here is what is supposed to happen:

on button click, the submit of the form is disabled. then a text swap runs. Upon the completion of the text swap, the var of cancel should switch to false and then submit the form.

However, the problem arises when the var cancel fails to switch to false. I am not sure why it won't.

<script type="text/javascript">
var cancel = true;

$("#btn-submit").click(function(){
  if (cancel = true) {
    $('form').on('submit', function(ev) {
      ev.preventDefault();
      form = $(".submit-form");
      fp = $('#free-planner');
      fp.fadeOut(350, function () {
        fp.text('Check your email!');
      });
      fp.fadeIn(350, function() { 
        var cancel = false; 
      });
    });
  }
}); 
if (cancel = false) {
  form.submit();
}
</script>

Upvotes: 0

Views: 178

Answers (4)

James Montagne
James Montagne

Reputation: 78630

if (cancel = false) {

should be

if (cancel == false) {

cancel = false is assignment and is always false.

Upvotes: 4

totten
totten

Reputation: 2817

<script type="text/javascript">
var cancel = true;

$("#btn-submit").click(function(){
  if (cancel) {
    $('form').on('submit', function(ev) {
      ev.preventDefault();
      form = $(".submit-form");
      fp = $('#free-planner');
      fp.fadeOut(350, function () {
        fp.text('Check your email!');
      });
      fp.fadeIn(350, function() { 
        var cancel = false; 
      });
    });
  }
}); 
if (!cancel) {
  form.submit();
}
</script>

Upvotes: 0

user188654
user188654

Reputation:

You should use comparison instead of assignment inside your if condition statements.

This is what you have...

if (cancel = true) {

and this is how it should look like

if (cancel == true) {

Though you could easily omit the right side of the statement

if(cancel){

Upvotes: 1

mgraph
mgraph

Reputation: 15338

not

if (cancel = true) {...
if (cancel = false) {...

but

if (cancel == true) {...
if (cancel == false) {...

or better:

if (cancel === true) {...
if (cancel === false) {...

Upvotes: 3

Related Questions