I am working on one JavaScript project where I need to switch between degrees Celsius and Fahrenheit. HTML is here
<button onclick="toggleCF();" id="toggleCF" class="button">Toggle C/F</button>
And here is the JavaScript function
this.toggleCF = function() {
console.log('click');
var fahrenheit = document.getElementById('toggleFahrenheit');
var celsius = document.getElementById('toggleCelsius');
if (fahrenheit.style.display === 'none') {
fahrenheit.style.display = 'block';
celsius.style.display = 'none';
} else {
fahrenheit.style.display = 'none';
celsius.style.display = 'block';
}
}
CSS i used
.temperature-celsius {
}
.temperature-fahrenheit {
display: none;
}
If you want to check out this application live here link
Please click on this link to see the application in a running form.
If you follow the link above and check, you will find that the switch does not work the first time you click. But when you click a second time, it starts working fine.
source
share