after clicking on the Celsius or Fahrenheit icon, i want to change the degree and the icon . What happens with me that its just updating it once.
html :
<ul id="list">
<li></li>
<li> <i class="wi wi-celsius"></i></li>
<li></li>
</ul>
JS:
$('#list li:nth-child(2)>i').click(function() {
var temp = $('#list li:nth-child(2)');
if ($(this).hasClass('wi wi-celsius')) {
alert("C");
var convertedTemp = parseInt(temp.text()) * 9 / 5 + 32;
temp.html(Math.round(convertedTemp) + ' <i class="wi wi-fahrenheit"></i>');
} else {
alert("F");
var convertedTemp = (parseInt(temp.text()) - 32) / (9 / 5);
temp.html(Math.round(convertedTemp) + ' <i class="wi wi- celsius"></i>');
}
});
Because you remove the element from the page. The event does not get hooked up to the new element that you add. So you need to use event delegation.
The other option is do not replace the HTML, just replace the text and classes.