Difficulty setting focus on newly created object in javascript -


so, related earlier question, forgive me naive javascript ways. want automatically bring text input focus when added dom. part of me thinks might trying add focus object before exists, i'm not quite sure how go fixing it. right relevant code:

var searchwrapper = document.createelement("div"); searchwrapper.id = "search-wrapper";  this.parentnode.replacechild(searchwrapper, this); document.getelementbyid("search-wrapper").focus(); 

but it's not quite working. should setting focus callback on replacechild, or there other way this?

try following:

live demo

var searchorig = document.getelementbyid("search-wrapper");   var searchwrapper = document.createelement("div"); searchwrapper.id = "search-wrapper"; searchwrapper.setattribute('tabindex', '0');   searchorig.parentelement.replacechild(searchwrapper, searchorig); document.getelementbyid("search-wrapper").focus(); 

Comments