Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to make a div blur or focus?

If i give a div a focus/blur event handler, when will the handler (if ever) run?

like image 880
Krewr Avatar asked Jul 19 '11 09:07

Krewr


1 Answers

If you give the div a tabindex attribute, it will be able to accept focus:

<div id="example" tabindex="0">Random content</div>

You can then attach focus and blur event handlers as you normally would. For example:

document.getElementById("example").onfocus = function() {
    console.log("focused");
}

Focus will be given to the div when you click on it, and blur will trigger when you click on any other element.

like image 93
James Allardice Avatar answered Nov 14 '22 23:11

James Allardice