Sorry if this obvious, I’m new to Javascript. I’m trying to make a calculator that calculates slope from two points. I couldn’t figure out how to clear text from one text input box, so I have four text input boxes but just one showing at a time. After pressing enter,the y1 function is called. Once you press enter again, the y1 function should call the x2 function, but it doesn’t. I used console.log to figure out that the y1 function isn’t detecting the keypress. I copy-pasted the detect keypress code from Stack Overflow and I don’t really understand it that well, so that might be part of it.
I tried getting rid of the function and moving the y1 code directly inside the if statement of the x1 part, but that didn’t work either.
Here is the HTML:
<p id="prompt">Loading...</p> <input type="text" id="input"> <input type="text" id="input2"> <input type="text" id="input3"> <input type="text" id="input4">
and here is the Javascript:
var x4 = document.getElementById('input4');
x4.style.display = "none";
var x3 = document.getElementById('input3');
x3.style.display = "none";
var x2 = document.getElementById('input2');
x2.style.display = "none";
var x = document.getElementById('input');
x.style.display = "inline";
document.getElementById("prompt").innerHTML = ("Please Enter x1");
const x1 = document.getElementById('input');
x1.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
y1()
}
});
function y1() {
document.getElementById('prompt').innerHTML = ("Please Enter y1");
x.style.display = "none";
x2.style.display = "inline";
const y1 = document.getElementById('input2');
x1.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
x2()
}
});
}
function x2() {
document.getElementById('prompt').innerHTML = ("Please Enter x2");
x2.style.display = "none";
x3.style.display = "inline";
const x2 = document.getElementById('input3');
x2.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
y2()
}
});
}
function y2() {
document.getElementById('prompt').innerHTML = ("Please Enter y1");
x3.style.display = "none";
x4.style.display = "inline";
const y2 = document.getElementById('input4');
y2.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
x3.style.display = "none"
document.getElementById('prompt').innerHTML = ("Loading");
}
});
}
I keep finding things that say I should use jquery but I don’t know much about it and would prefer not to.
2
Answers
Welcome to Stack Overflow,
Avoid naming your variables and functions the same.
In the future, post the error messages you encountered
const x1 = document.getElementById('input');
– no need to store it again, just usex.addEventListener('keydown'....
On function y1
x1.addEventListener("keydown", (e) => {
– you didn’t add event listener to y1, but to x1 that’s it doesn’t call it after you press enterIf you are ok with having 4 different boxes, you could implement something like this (it is only an idea):
DEMO:
The javascript function to check for input and allow only float numbers (which I believe is what you want) is extrapolated from this answer: https://stackoverflow.com/a/469362/21024780