jQuery Click Event triggering only once -
for reason, can click event fire once on temperature element (id: #temp). after converts fahrenheit, doesn't convert celsius. started jquery, appreciated. source code below.
html source:
<html> <head> <link rel="stylesheet" href="main.css"/> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-dzankj/6xz9si04hgrsxu/8s717jcizly3oi35eouye=" crossorigin="anonymous"></script> <script src="main.js"></script> <script type="text/javascript" async src="https://platform.twitter.com/widgets.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-bvyiisifek1dgmjrakycuhahrg32omucww7on3rydg4va+pmstsz/k68vbdejh4u" crossorigin="anonymous"/> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"></link> </head> <body> <h1>local weather code camp project</h1> <h4>location</h4> <p id="location"></p> <h4>weather</h4> <p id="weather"></p> <img id="icon"> <h3 id="temp"></h3> <p id="check"></p> </body> </html>
jquery source:
$(document).ready(function () { navigator.geolocation.getcurrentposition(function(position) { $.getjson('https://fcc-weather-api.glitch.me/api/current?lat=' + position.coords.latitude + '&lon=' + position.coords.longitude , function(data){ var tmpint = 0; $("h1").html(json.stringify(data)); $("#location").html(data.name); $("#weather").html(data.weather[0].description); $("#icon").attr("src", data.weather[0].icon); $("#temp").html(data.main.temp + "° c"); $(document).on('click','#temp',function() { tmpint = data.main.temp; tmpint = tmpint * 1.8 +32; if("#temp:contains('° c')") { $("#temp").html(tmpint + "° f"); $("#check").html("contains c"); } else if("#temp:contains('° f')") { $("#temp").html(data.main.temp + "° c"); $("#check").html("contains f"); } }); }); }); });
working codepen here.
the condition evaluating true the temp changed f.
using variable remember unit (c or f) displayed works...
var units = 'c'; $("#temp").on("click", function() { tmpint = data.main.temp; tmpint = tmpint * 1.8 + 32; if (units == 'c') { $("#temp").html(tmpint + "° f"); units = 'f'; } else if (units == 'f') { $("#temp").html(data.main.temp + "° c"); units = 'c'; } });
Comments
Post a Comment