I have been working on this issue for several days and researched it on SO, as well as on the Internet in general, and could not find material that helped me solve it.
I am trying to create a weather application that can switch the meteorological units displayed between Fahrenheit and Celsius. I start by adding weather to Fahrenheit, and then create an event handler that conditionally changes the internal content of the related item based on whether that item currently displays βFβ or βCβ.
Like this, my application successfully loads with Fahrenheit temperature and switches to Celsius by click, but it does not switch to Fahrenheit. I suppose there is some problem with how events are recorded, but for life I can not understand.
Here is my code:
var fahr = document.createElement("a"); fahr.attr = ("href", "#"); fahr.className = "tempUnit"; fahr.innerHTML = tempf + "°F" + "<br/>"; $("#currentWeather").append(fahr); var cels = document.createElement("a"); cels.attr = ("href", "#"); cels.className = "tempUnit"; cels.innerHTML = tempc + "°C" + "<br/>"; var units = document.getElementsByClassName("tempUnit"); $(".tempUnit").click(function() { if (units[0].innerHTML.indexOf("F") != -1) { $(".tempUnit").replaceWith(cels); } else { $(".tempUnit").replaceWith(fahr); } })
Thank you very much! We would like to provide additional information if necessary.
javascript jquery html
forrestbliss
source share