javascript - .focus() Not Focusing On New Element -


i have function renaming divs. way i'm trying work this:

  • user right clicks div
  • user clicks 'rename' on context menu
  • div gets input element , automatically focused
  • the input gets submitted after pressing enter or clicking screen

i have of steps done input element not being focused after click 'rename'. here's code:

function rename( ){     clickedfile.innerhtml = "<input class='rename' type='text' value='" + clickedfile.innerhtml + "'>";     clickedfile.childnodes[0].focus(); } 

the clickedfile node right clicked. changing innerhtml works fine .focus() not , i'm not sure why. don't errors on console, either.

i've tried using:

clickedfile.childnodes[0].select(); clickedfile.childnodes[1].focus(); clickedfile.focus(); 

none of them have worked.

edit:

  • i know using jquery might help, i'm more interested in finding out why isn't working.

  • i fixed problem. has event handlers. answer posted below.

you have add element part of dom

var input = document.createelement("input"); input.classname = "rename"; input.type = "text";  document.getelementbyid("somenode").appendchild(input); input.focus(); // should work 

see the fiddle


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -