Answer on Question #47635, Programming, AJAX | JavaScript | HTML | PHP
Problem.
<!DOCTYPE html>
<html>
<head>
<title>Temperature Converter</title>
</head>
<body>
<p>Enter a Temperature to Convert in the field below and select the starting scale.</p>
<input name="txtTemp" type="text" id="txtTemp" placeholder="Enter a Temperature"/>
<br/>
<br/>
<input id="radFahrenheit" name="radTemp" type="radio" checked="checked"/>Fahrenheit to Celcius
<br/>
<br/>
<input name="radTemp" type="radio" id="radCelcius" name="radTemp"/>Celcius to Fahrenheit
<br/>
<br/>
<div id="divResults"></div>
<br/>
<br/>
<button onclick="ConvertTemp();">Convert</button>
<script src="Week7.js" type="text/javascript"></script>
</body>
</html>Solution.
It is better to include javascript code at the end of the body to ensure that the DOM is loaded.
Code (Week7.js)
function ConvertTemp() {
var fahrenheit = document.getElementById('radFahrenheit').checked;
var temperature = document.getElementById('txtTemp').value;
var outputDIV = document.getElementById('divResults');
if (fahrenheit) {
outputDIV.innerHTML = (5/9) * (temperature - 32);
} else {
outputDIV.innerHTML = temperature * (9/5) + 32;
}
}Code (Question #47585.)
<!DOCTYPE html>
<html>
<head>
<title>Temperature Converter</title>
</head>
<body>
<p>Enter a Temperature to Convert in the field below and select the starting scale.</p>
<input name="txtTemp" type="text" id="txtTemp" placeholder="Enter a Temperature"/>
<br/>
<br/>
<input id="radFahrenheit" name="radTemp" type="radio" checked="checked"/>Fahrenheit to Celcius
<br/>
<br/>
<input name="radTemp" type="radio" id="radCelcius" value="0" type="radio"/>Celcius to Fahrenheit
<br/>
<br/>
<div id="divResults"></div>
<br/>
<br/>
<button onclick="ConvertTemp();">Convert</button>
<script src="Week7.js" type="text/javascript"></script>
</body>
</html>Result
Enter a Temperature to Convert in the field below and select the starting scale.
27
- ☐ Fahrenheit to Celcius
- ☀ Celcius to Fahrenheit
$0.6
Convert
http://www.AssignmentExpert.com/
Comments