Button will only work once

Button will only work once
0

#1

Right now I’m trying to get the F/C button to switch the 45 between 0 and 1, but right now it will only switch once, I would appreciate any help. Here is the code: https://codepen.io/jlayton75/full/jzzoLJ


#2

OK, first of all, why do you hate life? There is no reason to write:

$("#FC").click(function(i){i=0;if (i=0){var i=1;} else if(i=1){var i=0;$("#weather").html(i)}});
;

How can you read that?

If put in a more normal format, we get:

$("#FC").click(function(i) {
  i=0;
  if (i=0) {
    var i=1;
  } else if (i=1) {
    var i=0; 
    $("#weather").html(i)
  }
});

Isn’t that easier to read?

There are a few problems here.

  1. There is no need for i to be passed in the callback function.
  2. i should be declared globally, outside of the callback, otherwise it is creating a new one every time it is called.
  3. When you compare values, you use “===” or “==” - a single equals is for assignment.
  4. Inside the if and else sections, get rid of the var - that is for the initial declaration (see step 2)
  5. There is no need for the “else if”, “else” would be sufficient.
  6. The $("#weather").html(i) should be outside of the else block. Otherwise it would only run in the else block. You want it down one line.

When I make those changes, it works for me.


#3

With regards to #1, on reflection, what was happening is that you were actually passing in event data from jQuery into i that was getting written over by the i=0 statement.


#4

Thank you so much! That fixed it!