Changing from C to F

Changing from C to F
0

#1

I’ve coded for converting from Celsius to Fahrenheit, but on detecting the click (line 120) the page reloads with the default, which I’ve set to be Celsius (line 116). The console log shows that it’s passing through the code to change C to F.

I can’t see how to set an initial default (‘C’), but from then on, use F if the user has clicked on the C to request Fahrenheit. I’m trying to get it to toggle, but instead line 116 reloads the page and sets the scale indicator back to Celcius.

Here’s the code: http://codepen.io/roygrubb/pen/rrmPzA

What should I be doing here, please?


#2

Don’t use <a> tag to style C/F, but if you are using it, add href="#".

Also where are you declaring scaleInd (couldn’t find)?


#3

Good catch, thanks. I was originally going to use an anchor to change the scale, then forgot to remove the <a>

I’m not declaring scaleInd because I intend it to be global. Not good practice I know, but couldn’t find how to access it across the various functions otherwise.

I’m now going to try passing it in and also see if I can initialize it only when it’s undeclared.


#4

The undefined check proved to be the answer to my original question,
$(document).ready(function () { if ( typeof scaleInd === 'undefined') { var scaleInd = "C"; // indicates Celsius or Fahrenheit } etc.
and I now see out how to avoid making scaleInd global by passing it around.