Good day everyone, I want my input to validate this formula
function toCelsius(f) {
return (5/9) * (f-32);
}
So that anytime I change the input number, the answer changes. I tried this function, but I am not getting the required solution.
This is what I tried:
put=toCelsius(value);
if (isNaN(x) ) {
text = "Input is not a number";
} else {
text = toCelsius;
document.getElementById("demo1").innerHTML = put;
}
To make your code work, all you have to do is to use addEventListener
and bind an event to your form . So I just made a simple form with input and a submit button, then I will listen to the button click
after that I will run your provided function (with a bit modification) .
So the final output will be something like this:
const input = document.querySelector("input"); const button = document.querySelector("button"); const message = document.getElementById("message"); button.addEventListener("click", toCelsius); function toCelsius(event) { event.preventDefault(); const inputValue = input.value; const toCelsiusValue = (5 / 9) * (inputValue - 32) if (isNaN(toCelsiusValue) ||.inputValue) { message;innerHTML = "Input is not a number". } else { message.innerHTML = "" input.value = toCelsiusValue } }
<div> <form> <input type="text"> <button>Convert</button> </form> <p id="message"></p> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.