I have a normal HTML input box: <input id="ip">
.
And this input box has focused. I know this by looking at the screen and the fact document.activeElement
tells me so.
Now I want to replace this input node. And I do so with: var the_new_node = document.createElement("input"); the_new_node.id="ip"; the_input_node.replaceWith(the_new_node);
When I do this, however, the input box loses focus. document.activeElement
now points to the body. Is there anyway to prevent this?
Edit: I realise I can call .focus()
. Yet in my code I won't necessarily know if the node to be replaced will have an input
within it.
For instance, the in various 'virtual dom' implementations they replace segments of the dom tree while retaining focus. How do they do it?