how to focus an element created on the fly?
Just call .focus()
on the element after it's added to the DOM, for example:
var input = document.createElement("input"); //create it
document.body.appendChild(input); //append it
input.focus(); //focus it
You can test it out here.
The focus method will do this. If you have a reference to the newly-created element called elem
, then simply invoke:
elem.focus();
Note that you'll need to do this after inserting the element into the document at the appropriate point, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With