Search code examples
javascriptfocus

Is it possible to focus on a <div> using JavaScript focus() function?


Is it possible to focus on a <div> using JavaScript focus() function?

I have a <div> tag

<div id="tries">You have 3 tries left</div>

I am trying to focus on the above <div> using :

document.getElementById('tries').focus();

But it doesn't work. Could someone suggest something....?


Solution

  • window.location.hash = '#tries';
    

    This will scroll to the element in question, essentially "focus"ing it.