Skip to content
Advertisement

How can i set focus on a newly (automatically) rendered dom element?

I have an input field that gets replaced automatically with a textarea and same content depending on the number of characters the user has entered:

JavaScript

The problem i have is that the focus gets lost when a user enters the 21st character. And thus the user gets irritated because when he types the 22nd character it does not appear in the textarea (no focus). How can i set the focus on the newly rendered textarea then? Problem here is that it gets rendered automatically. Otherwise i could set a ref on the textarea and call focus().

Another issue is the removal of the 21st character and the switch-back from textarea to the input elment.

Advertisement

Answer

You could wrap the textarea/input in a component, and use its mounted hook to call its focus(), as seen in this component:

JavaScript
JavaScript

demo

While this is technically possible, this UX is probably not ideal, and you should consider other designs that don’t require focusing input like this (as indicated by @kien_coi_1997).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement