The .focus() method .focus() not work for the Input.(id="renameaction") Element Input.(id="renameaction") .

Called when the button is clicked (div id="win_rename") .

This element (id="renameaction") disappears and appears using display:none/display:block .

After appearing (renameshow is being accessed) it should appear and user input should be focused on this element.

browser chrome, os win 7.

It works only for the first time - at startup, I assume, because of the presence of the autofocus attribute, if the element is hidden and shown again, then there will be no focusing on it.

How to solve and what is wrong?

 <div id="win_rename" value='' onmousedown='renameshow()'>Переименовать</div> ... <input type='text' autofocus style='display:none;border #000 solid 1px;' id="renameaction" onkeypress="if (event.keyCode == 13) rename();" value="null" > ... function renameshow(){ disableactions(); var rel = document.getElementById("renameaction"); rel.style.zIndex = '5000'; rel.style.display = 'block'; rel.style.position = 'absolute'; var el = document.getElementById("contentelementselected"); if (!el) var el = document.getElementById("contentfolderselected"); rel.style.width = parseInt(winquery[0].wincontent.style.width)-44; rel.value = el.innerHTML; winaction.elementid = el; rel.style.height=19; el.appendChild(rel); rel.focus(); } function disableactions(){ var back = document.getElementById("win_back"); var open = document.getElementById("win_open"); var openInExplorer = document.getElementById("win_openInExplorer"); var copy = document.getElementById("win_copy"); var paste = document.getElementById("win_paste"); var filedelete = document.getElementById("win_delete"); var rename = document.getElementById("win_rename"); back.style.display='block'; open.style.display='none'; openInExplorer.style.display='none'; rename.style.display='none'; filedelete.style.display='none'; copy.style.display='none'; if (copyobj=='null') paste.style.display='none'; else paste.style.display='block'; var addfav = document.getElementById("win_addfav"); addfav.style.display='none'; winaction.addfav.style.display='none'; } 

Full code: https://dl.dropboxusercontent.com/u/44864159/www.rar

  • The full code is better to place on jsfiddle.net - Nasty Pacman
  • It will not work on jsfiddle, everything is in one file - index.php and, in addition to html, there is php, just tried it - wrote a bunch of errors. - infar
  • @infar, you write that you have a problem in js, put a lot of js-code in the message body, and when it comes to feedl, you start downloading php, why? The problem is in js, so load only js on feedl. On the vault, this solution can help you put the focus on the field: stackoverflow.com/a/10038850 - MasterAlex
  • Thanks, if you do this setTimeout (function () {document.getElementById ('renameaction'). Focus ();}, 0); it works. - infar

0