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.)
47 Replies
<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
š
You don't use
.innerHTML
to get the value out of an input field, you have to use .value
ohhhhhh
thanks!
someone was telling me about how u need .value
but he said to get verification
so i joined this server
thanks š
Seems like you're also missing a "}"
where?
I think that's just indenting issues
oh
let me check
thanks š
Aah
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
joch is right
it's indentation problem
oh ok
noted
Or no actually, i think it is missing hmm..
š
what is it missing from
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
from else
its not
ok i'll see if I can
that'll be a huge help
Yeah i see
You should write it like this
Now it's much easier to see
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
innerHTML changes the values between the opening and closing tag, but if you want the value from an input you use value
ok
so
I implemented it into my code
.value i mean
so that should work
oh ok
thanks
appreciate it
I'll try it and report back to u š
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
oh ok
its fine
thanks!
damit doesnt work
can anyone else help me?
check your console for errors
ok
there is none
š¦
is anyone else available that can possibly help me with this question
Send me the full code
Is it still this one?
the full code?
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
yep
here ill give it
And did you have any errors?
<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
ā¬ļø
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 ''
)ohhhhh
thank u so much!
:ThumbsUp:
what type of coding do u do?
For fun? All kinds, but mostly python / webdev fullstack with js. For work? The boring kind: migration of applications from COBOL to Java
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
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
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
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
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