henhen henhen - 1 month ago 6
HTML Question

Hide html elements after a key is pressed in javascript

I was wondering if there was a way to hide header tags, images, divs etc from showing on an html page after a certain key is pressed. I am making a hangman game and when the player loses or wins, I display a message with a header tag. If the user presses Y, the game restarts but I dont how to make the header tag that ask "play again" to disappear once the player chooses Y

Answer

Hey there are several methods for doing this I am just showing here a keypress example for Enter button click lets try the below code

$(document).keypress(function(e) {
  if(e.which == 13) {    //for enter button
    $('#div_name').hide();
}
 });