How to make input autofocus in internet explorer?

Here’s a one-liner (well, one line of actual logic) that uses jQuery to make autofocus work in IE. It bails out if the focus is already set–in other words, in any HTML5-capable browser.

$(function() {
  $('[autofocus]:not(:focus)').eq(0).focus();
});

Here is an updated jsFiddle that works in IE.

Leave a Comment