Reputation: 201
I am working on a simple conversion form from celsius to fahrenheit and from fahrenheit to celsius. I can't figure out why it is not converting.
HTML
<head>
<script type="text/javascript" src="script.js"></script>
</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" onclick="processForm()">
</form>
</body>
Javascript function processForm() {
var temperature = Number(document.tempForm.temp.value);
var tempType;
var result;
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
result = document.tempForm.resultField.value;
}
Upvotes: 1
Views: 275
Reputation: 2562
Your conversion is working but you are assigning the result for resultField
in a wrong way.
Convert the assignment (the last one) like this
document.tempForm.resultField.value = result;
Upvotes: 1
Reputation: 42444
You are assigning the result the wrong away around at the end. You have to put the target of the assignment on the lefthandside of the assingment, hence you resultfield and on the righthandside the value you want to assign it to, like so:
document.tempForm.resultField.value = result;
Upvotes: 5