Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Not sure why this JSON isn't working.

I have a $.getJSON() that sends out some json and pretty prints to the page. Then I have another $.getJSON(), but it only operates when the button it is appended to, is clicked: $('.button').click(function in here). So that works great, but now how do I get it to go back to the first getJSON, the one without a click event. I'm making a Celsius to Fahrenheit button. The first .getJSON loads the Celsius. The 2nd getJSON loads the Fahrenheit and switches from C to F when clicked, just like it should. But then if I click the button again, it doesn't go back to C and load the said Celsius. Awfully worded question but would appreciate help.

Can you post your code?

All you need, I assume is the JS tray, but here's the entire link: http://codepen.io/xGabey/pen/NbLzZo?editors=0010

I just used Boston as a supplied location, but I'm going to replace it soon with geolocation navigator. The temperature toggle is what I need help with right now, though. Thanks!

~ Gabriel

1 Answer

On line 26 in the JavaScript file you are sending impUnits regardless of the state of the checkbox. You need some logic to check the value of the checkbox and then switch out the units variable depending on its state before you send the AJAX request.

As a side note, it might not be ideal to send a new request to the server and have the client wait for a response just to convert between metric and imperial units, which is math that you can do on the client side. There is no new information. But I understand that this is probably an exercise in AJAX requests and it's not about that.