Try adding javascript soon after the input element so that it runs until the page loads. In your case, the autofocus attribute is set to this element, but the focus on the element that autofocus has on the browser is already done. therefore, you set the value after the browser has set focus. when the browser does not try to set it without an attribute. try the following code
<input type="text"> <script> document.querySelector('input').setAttribute('autofocus', 'autofocus'); </script>
http://jsbin.com/yatugaxe/1/
If you need to do this with the click of a button or something else, you need to do it in JavaScript. setting an attribute does not mean that the browser will execute it at any time.
source share