2- condition is wrong if (event.keyCode == 27) && hidden = "true" { the && hidden="true" is outside bracket and also hidden="true"means you are giving hidden a new value, not asking if the value of hidden is true so you have to use == for comparisons
3- No need for 2 onkeyup functions, just use and else statement
I didn’t understand much what you are trying to do, but your code has some errors, if you want to do a visibility toggle this is the code you need I think
Here is another take of it improving on previus answers and making the code ES6 (last version of javascript). As well as some comments on the code I added.
<script>
// Get the div element and store a reference to it in box variable
const box = document.getElementById("div");
window.addEventListener("keyup", (e) => {
// if 'Esc' is pressed AND the visibility is NOT "hidden"
if (event.keyCode === 27 && box.style.visibility !== "hidden") {
// set visibility to "hidden"
box.style.visibility = "hidden";
}
})
</script>
Not sure why but unhide was requested as well
<script>
// Get the div element and store a reference to it in box variable
const box = document.getElementById("div");
// store the original visibility setting, better in case you change it in the future using html.
let boxStateOrg = box.style.visibility;
window.addEventListener("keyup", (e) => {
// if 'Esc' is pressed AND the visibility is NOT "hidden"
if (event.keyCode === 27) {
if (box.style.visibility !== "hidden") {
// set visibility to "hidden"
box.style.visibility = "hidden";
} else {
box.style.visibility = boxStateOrg;
}
}
})
</script>
Problem :
I want to make it so when someone clicks the escape key it will hide the tag. how would I do that?
Here is my current code:
Thank you all for the help! i got many answers, I didn’t notice everything I did wrong, I will check the answers and see what works! Sorry if I wasn’t clear, I was just trying to hide the tag.
Comments
Comment posted by Emiel Zuurbier
Could you specify what exact part isn’t working and the errors you’re encountering?
Comment posted by David
The code is throwing syntax errors.
Comment posted by Chris G
your conditions are wrong on both
Comment posted by Logan Latham
For some reason this didn’t work, thank you though!