JSFiddle - React, Tailwind, and code Playground
by staeff
HTML
<html>
<head>
</head>
<body>
<form name="tempForm">
<label for="temp">Temperature:</label>
<input type="text" id="temp"><br>
<input type="radio" name="choice" value="fahrenheit" checked />Convert to Fahrenheit <br>
<input type="radio" name="choice" value="celsius">Convert to Celsius <br>
<label for="resultField">Result: </label>
<input type="text" id="resultField"><br>
<input type="button" value="Convert" id="submit">
</form>
</body>
</html>
JavaScript
// Celsius/Fahrenheit - Konverter
document.getElementById("submit").onclick = function() {
processForm();
}
function processForm() {
var temperature = Number(document.tempForm.temp.value);
var tempType;
var result;
// Determine selected checkbox
for (var i=0; i < document.tempForm.choice.length; i++) {
if (document.tempForm.choice[i].checked) {
tempType = document.tempForm.choice[i].value;
}
}
if (tempType == 'fahrenheit') {
result = temperature * 9/5 + 32;
} else {
result = (temperature - 32) * 5/9;
}
// Assign the result field value here
document.tempForm.resultField.value = result;
}