Making output display from javascript function and onkeydown
Making output display from javascript function and onkeydown
I have a question about making an output display. My code works for the most part, but only after I delete the input. For example, when I type in "0", nothing happens, but after I delete the 0 in the input, only then will it display "100%". I'm trying to make it display "100%" as soon as I put "0" in the input. To be clear, I'm not attempting to make a calculator. I'll be putting in other outputs aside from percentages.
document.getElementById("myscore").onkeydown = function() {
myJsFunction()
};
function myJsFunction() {
var numberwrong = document.getElementById('myscore').value;
if (numberwrong == 0) {
score = "100%";
} else if (numberwrong == 1) {
score = "95%";
} else if (numberwrong == 2) {
score = "90%";
}
document.getElementById("totalscore").innerHTML = score;
}
<p2>Number Incorrect out of 50: </p2>
<input type="number" id="myscore">
<p id="totalscore"></p>
100%
p2
h2
onkeydown
element.addEventListener()
script
body
@ScottMarcus Type
1
into the box and it will display 100%
immediately, not 95%
– Barmar
Jun 30 at 1:06
1
100%
95%
Hmm.. when I input "2" and hit enter, it shows the correct value (90%). But it initially shows "100%" when I type in 2 before I hit enter. I'm trying to avoid it saying the incorrect value at first. Thanks for your fix on the other piece of code.
– Jon
Jun 30 at 1:07
@Barmar The question states that upon entering
0
nothing happens and that the OP wants 100%
to display immediately after typing 0
. The code does this already. You are describing a logic error in the calculation, but that isn't what the question is asking about.– Scott Marcus
Jun 30 at 1:07
0
100%
0
1 Answer
1
Use onkeyup
rather than onkeydown
. Your code is running before you change the value of the input.
onkeyup
onkeydown
document.getElementById("myscore").onkeyup = function() {
myJsFunction()
};
function myJsFunction() {
var numberwrong = document.getElementById('myscore').value;
if (numberwrong == 0) {
score = "100%";
} else if (numberwrong == 1) {
score = "95%";
} else if (numberwrong == 2) {
score = "90%";
}
document.getElementById("totalscore").innerHTML = score;
}
<p2>Number Incorrect out of 50: </p2>
<input type="number" id="myscore">
<p id="totalscore"></p>
I'm trying to make it display "100%" as soon as I put "0" in the input. The original code already does this.
– Scott Marcus
Jun 30 at 1:08
It also does that when you put 1 into the input, which is obviously wrong, since it's supposed to display 95%.
– Barmar
Jun 30 at 1:10
Thank you! Elegant solution. You are awesome! I appreciate the help.
– Jon
Jun 30 at 1:16
when I type in "0", nothing happens But, that's not accurate. The current code does produce output.
– Scott Marcus
Jun 30 at 1:17
So sue me for answering the question he obviously meant to ask, because he didn't express the problem perfectly.
– Barmar
Jun 30 at 1:18
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code works as is. When I type 0 into the box
100%
appears immediately. So, I'm voting to close as your problem is not reproducible. Having said that, your HTML is invalid, there is no such tag asp2
(there is anh2
). Also, you should separate your JavaScript from your HTML and not use inline HTML event attributes (onkeydown
) and instead useelement.addEventListener()
. The only thing I can think might be your issue is that yourscript
element should come just before your closingbody
tag so that by the time the script is encountered, the HTML is in memory.– Scott Marcus
Jun 30 at 1:04