Button will only work once

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

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) {
  if (i=0) {
    var i=1;
  } else if (i=1) {
    var i=0; 

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.

1 Like

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.

Thank you so much! That fixed it!