Question about a bug in my website

Hi! I have a question about this ss from my code. I am COMPLETE beginner and I just started with coding. So far, I aspire to learn HTML, Css, and Javascript. I do know that there is a better way to do what I am doing in the picture, but I don't really know much so I'm just taking this way. Anyways, as you can see in the javascript, I tell it to assign the value keyCode to document.getElementById("keyPass").innerHTML and then I add a window alert to tell me what the value is. However, for some reason, it doesn't store the input data and it just returns that it was blank. Any fixes or problems you see in the code? ( will also be copy and pasting the code after this.)
No description
47 Replies
Sevrin
SevrinOPā€¢2w ago
<input id="keyPass" type="number"> <a id="linky" href="wrong.html" target="_blank">If you get this you will unlock the key</a> <button onclick="checkIfCorrect()">Check ur answer</button> <script> let keyCode = document.getElementById("keyPass").innerHTML; let shreyImage = document.getElementById("linky").innerHTML; function checkIfCorrect() { window.alert(keyCode) if (keyCode == 1049) { const element = document.getElementById("linky"); element.innerHTML = "Correct!"; element.href = "2Untitled.jpg"; } else { const element = document.getElementById("linky"); element.innerHTML = "Wrong, Try again" } } </script> wow this is a nice server such quick response time šŸ‘
Jochem
Jochemā€¢2w ago
You don't use .innerHTML to get the value out of an input field, you have to use .value
Sevrin
SevrinOPā€¢2w ago
ohhhhhh thanks! someone was telling me about how u need .value but he said to get verification so i joined this server thanks šŸ‘
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
Seems like you're also missing a "}"
Sevrin
SevrinOPā€¢2w ago
where?
Jochem
Jochemā€¢2w ago
I think that's just indenting issues
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
No description
Sevrin
SevrinOPā€¢2w ago
oh let me check thanks šŸ‘
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
Aah
Jochem
Jochemā€¢2w ago
pro-tip to get used to real early: Make sure you keep your indenting straight. It really helps with debugging, and having the indenting messed up makes your code look amateurish in a heartbeat
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
joch is right it's indentation problem
Sevrin
SevrinOPā€¢2w ago
oh ok noted
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
Or no actually, i think it is missing hmm..
Sevrin
SevrinOPā€¢2w ago
šŸ‘ what is it missing from
Jochem
Jochemā€¢2w ago
if you can, I'd recommend just installing something like Prettier and setting it up to autoformat on save, life's too short to worry about that shit manually, but even before you do it's really important to keep them straight. Pick a standard (doesn't matter which one) and follow it
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
from else
Sevrin
SevrinOPā€¢2w ago
its not
Sevrin
SevrinOPā€¢2w ago
No description
Sevrin
SevrinOPā€¢2w ago
ok i'll see if I can that'll be a huge help
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
Yeah i see You should write it like this
let keyCode = document.getElementById("keyPass").innerHTML;
let shreyImage = document.getElementById("linky").innerHTML;

function checkIfCorrect() {
window.alert(keyCode);
if (keyCode == 1049) {
const element = document.getElementById("linky");
element.innerHTML = "Correct!";
element.href = "2Untitled.jpg";
} else {
const element = document.getElementById("linky");
element.innerHTML = "Wrong, Try again";
}
}
let keyCode = document.getElementById("keyPass").innerHTML;
let shreyImage = document.getElementById("linky").innerHTML;

function checkIfCorrect() {
window.alert(keyCode);
if (keyCode == 1049) {
const element = document.getElementById("linky");
element.innerHTML = "Correct!";
element.href = "2Untitled.jpg";
} else {
const element = document.getElementById("linky");
element.innerHTML = "Wrong, Try again";
}
}
Now it's much easier to see
Sevrin
SevrinOPā€¢2w ago
okay! thank you! oh noooooo its not working wait Jochem the fix to the bug was to change innerhtml to value correct? its still giving me the windows alert with a blank value
Jochem
Jochemā€¢2w ago
innerHTML changes the values between the opening and closing tag, but if you want the value from an input you use value
Sevrin
SevrinOPā€¢2w ago
ok so I implemented it into my code
Jochem
Jochemā€¢2w ago
let keyCode = document.getElementById("keyPass").value;
let shreyImage = document.getElementById("linky").innerHTML;

function checkIfCorrect() {
window.alert(keyCode);
if (keyCode == 1049) {
const element = document.getElementById("linky");
element.innerHTML = "Correct!";
element.href = "2Untitled.jpg";
} else {
const element = document.getElementById("linky");
element.innerHTML = "Wrong, Try again";
}
}
let keyCode = document.getElementById("keyPass").value;
let shreyImage = document.getElementById("linky").innerHTML;

function checkIfCorrect() {
window.alert(keyCode);
if (keyCode == 1049) {
const element = document.getElementById("linky");
element.innerHTML = "Correct!";
element.href = "2Untitled.jpg";
} else {
const element = document.getElementById("linky");
element.innerHTML = "Wrong, Try again";
}
}
Sevrin
SevrinOPā€¢2w ago
.value i mean
Jochem
Jochemā€¢2w ago
so that should work
Sevrin
SevrinOPā€¢2w ago
oh ok thanks appreciate it I'll try it and report back to u šŸ‘
Jochem
Jochemā€¢2w ago
It'll be a pretty good while till I respond, I'm heading off for the night. Someone else might be able to help too though
Sevrin
SevrinOPā€¢2w ago
oh ok its fine thanks! damit doesnt work can anyone else help me?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
check your console for errors
Sevrin
SevrinOPā€¢2w ago
ok there is none šŸ˜¦ is anyone else available that can possibly help me with this question
vinter.
vinter.ā€¢2w ago
Send me the full code Is it still this one?
Sevrin
SevrinOPā€¢2w ago
the full code?
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
Did you check your console for errors? The best you can do is to first check for errors, if you have errors, post the error here in chat and we know immediately why it's not working. otherwise if there is no error then you can upload your code to codepen and share link here so we can try it and see why it does not work
Sevrin
SevrinOPā€¢2w ago
yep here ill give it
Tok124 (CSS Nerd)
Tok124 (CSS Nerd)ā€¢2w ago
And did you have any errors?
Sevrin
SevrinOPā€¢2w ago
<input id="keyPass" type="password"> <a id="linky" href="wrong.html" target="_blank">If you get this you will unlock the key</a> <button onclick="checkIfCorrect()">Check ur answer</button> <script> let keyCode = document.getElementById("keyPass").value; let shreyImage = document.getElementById("linky").innerHTML; function checkIfCorrect() { window.alert(keyCode) if (keyCode == 1049) { const element = document.getElementById("linky"); element.innerHTML = "Correct!"; element.href = "2Untitled.jpg"; } else { const element = document.getElementById("linky"); element.innerHTML = "Wrong, Try again" } } </script> no its the one i just posted above this ā¬†ļø
vinter.
vinter.ā€¢2w ago
You're assigning keyCode outside of checkifCorrect The assignment will happen when the <script> tag is loaded So it will be assigned to the default value of your <input> (aka '')
Sevrin
SevrinOPā€¢2w ago
ohhhhh thank u so much!
vinter.
vinter.ā€¢2w ago
:ThumbsUp:
Sevrin
SevrinOPā€¢2w ago
what type of coding do u do?
vinter.
vinter.ā€¢2w ago
For fun? All kinds, but mostly python / webdev fullstack with js. For work? The boring kind: migration of applications from COBOL to Java
Sevrin
SevrinOPā€¢2w ago
oh lol nice im also looking for work in comp sci department but im just a beginner coder so i still need to learn before getting a job
vinter.
vinter.ā€¢2w ago
Yeah, that's good! Keep practicing šŸ™‚ Doing personal projects is the best way to learn And you might also get something to show on the resume in the process
Sevrin
SevrinOPā€¢2w ago
yeah thats what I hope will happen but for now I need to focus on the basics from the code you could probably tell that i'm a beginner lol im sure there's lots of other ways to check the pasword of smth im just too amatuer to know them lol
Jochem
Jochemā€¢2w ago
just as a general FYI, checking passwords in the frontend isn't useful as a security measure. It's like using a paperclip to keep a document secure any "attacker" can just view-source and look at the keycode in your code, then type it in
Sevrin
SevrinOPā€¢2w ago
yeah ik but i'm actually making a scavenger hunt online and the intended solution is using view src to see the password thats reason i made it in front end oops forgot to mark as solved
Want results from more Discord servers?
Add your server