Can anyone help me identify what the problem is in my code?
HTML
<section>
<!--- Celsius, Fahrenheit, Kelvin -->
<table>
<tr>
<td>°C</td>
<td>°F</td>
<td>°K</td>
</tr>
<tr>
<td><label for="celsius"></label><input type="number" id="celsius"/> </td>
<td id="fahr1"></td>
<td id="kelv1"></td>
</tr>
<tr>
<td>later</td>
<td> <input type="number" id="fahrenheit"/> </td>
<td>later</td>
</tr>
</table>
</section>
<script src="js/main.js"></script>
and the JAVASCRIPT is
// Gets Celsius and returns Kelvin
let celsin1 = document.getElementById('celsius');
let fahrout1 = document.getElementById('fahr1');
let kelvout1 = document.getElementById('kelv1');
let newfahr1 = 0;
let newkelv1 = 0;
function celstokelv1(){ newkelv1 = celsin1 + 273;
return newkelv1}
function celstofahr1(){newfahr1 = (9/5) * celsin1 + 32;
return newfahr1}
function change1() {
fahrout1.innerHTML = '<td id="fahr1">'+ celstofahr1() +'</td>';
kelvout1.innerHTML = '<td id="kelv1">' + celstokelv1() + '</td>'
}
celsin1.addEventListener('change', change1, false);
If anyone can help me, it would be much appreciated.
Problems include:
When typing into the input bar, the other ones in the same row do not change.
Output for any number into the first Celcius input is NaN for Fahrenheit and
[object HTMLInputElement]273 for Kelvin.
.value- what you have done is retrieve a pointer to the input object that you wanted to pull a value from. However, attempting to do maths with that doesn't work and produces the NaN. As a general rule you need to use the value field whenever you are dealing with any type of input-type element (input, button, textarea, select).changeis only fired after losing focus (which some libraries like jQuery / React will change this behaviour). You may consider usinginputin youraddEventListener.