0

http://codepen.io/PaxBlueRibbon/pen/wWJWXj

I'm trying to teach myself Javascript, but this fahrenheit/celsius switch doesn't do anything. I set up my switch like this:

$(document).ready(function getPosition() {
  if (document.getElementById('myonoffswitch').checked) {
    var unit = "Fahrenheit";
  } else {
    var unit = "Celsius";
  }

and set conditionals in the JS like this

      var tempKelvin = json.main.temp;
      var tempFahrenheit = tempKelvin * 1.8 - 459.67;
      var tempCelsius = tempKelvin - 273.15;
      if (unit == "Fahrenheit") {
        $(".temp").html(Math.round(tempFahrenheit) + " Degrees " + unit);
      }
      if (unit == "Celsius") {
$(".temp").html(Math.round(tempCelsius) + " Degrees " + unit);
      }

The fahrenheit works, and if I change the variable to something else it stops working, but I can't get it to show Celsius.

4
  • You are only setting the value of unit once. You will need to change it when your myonoffswitch is changed. Commented Jul 11, 2016 at 21:11
  • is your switch a checkbox or radio button? I think you might need to check the status of your switch dynamically by using an event handler. Commented Jul 11, 2016 at 21:11
  • codepen.io/anon/pen/OXOVGa if you look at the console, you will note that the unit is being updated, however not being detected in the json function. I put a log in the json function to show the current unit and it always seemed to show Fahrenheit, regardless of change??? Commented Jul 11, 2016 at 22:47
  • i think you may have to pass it into the json function Commented Jul 11, 2016 at 23:00

1 Answer 1

1

Since you are using jQuery.

// you should add event listener.
$('#myonoffswitch').change(function(){
    // and change "unit" every time checkbox is changed
    unit = $(this).prop('checked') ? 'Fahrenheit' : 'Celsius';
    // change html here
});

And you can call this event on page load

$('#myonoffswitch').change();
Sign up to request clarification or add additional context in comments.

3 Comments

i'm trying to figure out where in my code to place these so they run at the proper time
I would suggest you to rearrange your code a bit. First define your global variables, like var unit, location; etc at the top and change each of them accordingly ( eq. checkbox changed, JSON loaded, etc.). Divide each action into separate functions. (eq. getLocation(), drawHTML(), etc). Call them whenever they are needed (eq. Page Loaded, checkbox changed, time intervar, etc) and let them use global variables defined earlier.
$(document).ready() specifies that everything inside it will be executed when the page loads. Always put event bindings inside it to assure that you are binding to an element that has been created.

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.