shouts
11-20-2012, 02:44 AM
Having trouble with this code... trying to change status of textboxes with onclick event of radio button - any tips?
<html>
<head>
<title>Convert Temperature</title>
</head>
<body>
<h1>
Convert Temperature</h1>
<input name="temperature" type="radio" id="rbCelsius" value="Celsius" onclick="setCelsius()" />
From Fahrenheit to Celsius<br />
<input type="radio" name="temperature" id="rbFahrenheit" value="Fahrenheit" onclick="setFahrenheit()" />
From Celsius to Fahrenheit<br />
<br />
Degrees Fahrenheit:   <input type="text" id="txtFahrenheit" />
<br />
Degrees Celsius:        <input type="text" id="txtCelsius" />
<p>
<input type="button" id="btnConvert" value="Convert" /></p>
<script type="text/javascript">
var $ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
$("btnConvert").onclick = Convert;
}
function Convert() {
if ($("rbCelsius").checked){
F = $("txtCelsius").value * 9 / 5 + 32;
$("Fahrenheit").value = Math.round(F);
}
else if ($("rbFahrenheit").checked){
C = $("txtFahrenheit").value -32) * 5 / 9;
$("Celsius").value = Math.round(C);
}
function setCelsius () {
// move spaces to both textboxes
$('txtFahrenheit').value = '';
$('txtCelsius').value = '';
// disable Celsius textbox
$("txtCelsius").disabled = true;
// enable Fahrenheit textbox
$("txtFahrenheit").disabled = false;
}
function setFahrenheit () {
// move spaces to both textboxes
$('txtFahrenheit').value = '';
$('txtCelsius').value = '';
// disable Fahrenheit textbox
$("txtFahrenheit").disabled = true;
// enable Celsius textbox
$("txtCelsius").disabled = false;
}
</script>
</body>
</html>
<html>
<head>
<title>Convert Temperature</title>
</head>
<body>
<h1>
Convert Temperature</h1>
<input name="temperature" type="radio" id="rbCelsius" value="Celsius" onclick="setCelsius()" />
From Fahrenheit to Celsius<br />
<input type="radio" name="temperature" id="rbFahrenheit" value="Fahrenheit" onclick="setFahrenheit()" />
From Celsius to Fahrenheit<br />
<br />
Degrees Fahrenheit:   <input type="text" id="txtFahrenheit" />
<br />
Degrees Celsius:        <input type="text" id="txtCelsius" />
<p>
<input type="button" id="btnConvert" value="Convert" /></p>
<script type="text/javascript">
var $ = function (id) {
return document.getElementById(id);
}
window.onload = function () {
$("btnConvert").onclick = Convert;
}
function Convert() {
if ($("rbCelsius").checked){
F = $("txtCelsius").value * 9 / 5 + 32;
$("Fahrenheit").value = Math.round(F);
}
else if ($("rbFahrenheit").checked){
C = $("txtFahrenheit").value -32) * 5 / 9;
$("Celsius").value = Math.round(C);
}
function setCelsius () {
// move spaces to both textboxes
$('txtFahrenheit').value = '';
$('txtCelsius').value = '';
// disable Celsius textbox
$("txtCelsius").disabled = true;
// enable Fahrenheit textbox
$("txtFahrenheit").disabled = false;
}
function setFahrenheit () {
// move spaces to both textboxes
$('txtFahrenheit').value = '';
$('txtCelsius').value = '';
// disable Fahrenheit textbox
$("txtFahrenheit").disabled = true;
// enable Celsius textbox
$("txtCelsius").disabled = false;
}
</script>
</body>
</html>