Focus an element created on the fly

How to focus an element created on the fly?

+3
source share
5 answers

Just call .focus()the element after adding it to the DOM, for example:

var input = document.createElement("input"); //create it
document.body.appendChild(input);            //append it
input.focus();                               //focus it

You can check it out here .

+6
source

This focus method will do this. If you have a link to a newly created item with a name elem, just call:

elem.focus();

Please note that you will need to do this after adding the element to the document at the appropriate point, of course.

+3
source

,

var txtObj = document.createElement("input");
window.document.body.appendChild(a);
txtObj.focus();
0
<script>    
  $('#container').append('<input type="text">');
  $('#container').find('input:last').focus();
</script>
0

setTImeOut

setTimeout(function() {
        $("#id_of_element_created").focus().select();
       }, 100);
-2

Source: https://habr.com/ru/post/1774912/


All Articles