Skip to content Skip to sidebar Skip to footer

Refresh Page On Enter / Return Key Press

I made a little quiz with HTML, CSS and JavaScript that helps me learn stuff for school. I only use it locally for myself. I'd like to make it possible to refresh the page with the

Solution 1:

You can put a listener on the document and it will intercept all keypresses, just make sure you're reloading the page when Enter is pressed:

document.addEventListener('keyup', function(e){
  if(e.keyCode == 13)
    window.location.reload();
})

Solution 2:

Either make your quiz a form submitted on 'enter' (as @miqdadamirali suggested), or attach an 'onkeypress' event to document body and use a little bit of javascript as a handler:

function handler(evt){
    if(evt.keyCode == 13) // enter key
        location.reload() // refresh page
}   

Solution 3:

Here you'll find a helpful key code generator and reference: http://www.javascriptkeycode.com

Post a Comment for "Refresh Page On Enter / Return Key Press"