r/learnjavascript • u/New-District7562 • Oct 01 '26
How to make different inputs respond based on one input?
I'm making a temp converter project (following 100JS) and for the life of me, I can't figure out how to get the values in the input boxes to change upon me typing a number into the first one. Anyone have any ideas? Here's where I'm at:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Temperature Converter</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<h1 class="heading">Temperature Converter</h1>
<div class="temp-container">
<label for="celsius">Celsius:</label>
<input
onchange="temperatureConversion(event)"
type="number"
name="celsius"
id="celsius"
placeholder="Enter Temperature"
class="input"
/>
</div>
<div class="temp-container">
<label for="fahrenheit">Fahrenheit:</label>
<input
type="number"
onchange="temperatureConversion(event)"
name="fahrenheit"
id="fahrenheit"
placeholder="Enter Temperature"
class="input"
/>
</div>
<div class="temp-container">
<label for="kelvin">Kelvin:</label>
<input
type="number"
onchange="temperatureConversion(event)"
name="kelvin"
id="kelvin"
placeholder="Enter Temperature"
class="input"
/>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
const celsius = document.getElementById("celsius");
const fahrenheit = document.getElementById("fahrenheit");
const kelvin = document.getElementById("kelvin");
const inputEl = document.querySelectorAll("input");
function temperatureConversion() {
const fahrenheitToCelsius = ((fahrenheit.value - 32) * 5) / 9;
console.log(fahrenheitToCelsius);
const fahrenheitToKelvin = ((fahrenheit.value - 32) * 5) / 9 + 273.15;
console.log(fahrenheitToKelvin);
const celsiusToFahrenheit = (celsius.value * 9) / 5 + 32;
console.log(celsiusToFahrenheit);
const celsiusToKelvin = celsius.value + 273.15;
console.log(celsiusToKelvin);
const kelvinToFahrenheit = ((kelvin.value - 273.15) * 9) / 5 + 32;
console.log(kelvinToFahrenheit);
const kelvinToCelsius = kelvin.value - 273.15;
console.log(kelvinToCelsius);
}