How to cache HTMLElement.focus in a variable?

98 Views Asked by At

What is the correct context to store the focus function of an HTMLElement in a variable?

I tried,

var elem = document.getElementById('elem');
var focus = elem.focus.bind(document); // focus() Illegal Invocation
var focus2 = elem.focus.bind(elem); // focus2() Illegal Invocation
1

There are 1 best solutions below

0
On

Wrap it into your own function:

var elem = document.getElementById('elem');
var focusElem = function(){ elem.focus(); };
focusElem();