How to automatically show textarea cursor?

Can you guys tell me how to automatically display the textarea cursor? Usually, when I click on textarea, I have a text cursor. Is it possible to show it right away?

+4
source share
5 answers

Set the autofocus attribute in the control. Please note that this requires a browser that supports this particular bit of the HTML 5 project.

 <textarea rows="4" cols="30" autofocus></textarea> 

You can also set it using JavaScript, but it can interfere with the normal use of the page (especially with screen readers that use the focus point for reading) (there may also be an autofocus attribute, but it is at least standard so the software for reading with screen can be written for its work).

JavaScript method that I do not recommend:

 <textarea rows="4" cols="30" id="mytextarea"></textarea> <script> document.getElementById('mytextarea').focus(); </script> 

Note that this one does not use the onload event. This event does not fire until the entire document is loaded, including dependencies, such as images, so there is usually a significant delay with which the user can start interacting with the page. Adjusting the focus after this point is likely to disrupt the fact that the user is in the middle.

+13
source

You can set the focus. If you are not using external libraries (e.g. jQuery), try the following:

 <body onload="document.yourForm.yourTextarea.focus();"> <form name="yourForm"> <textarea name="yourTextarea"></textarea> </form> </body> 
+1
source

You can specify focus in this text box during page load.

0
source

This is an HTMLElement called the focus() method.

Here's a link to an example: http://www.w3schools.com/jsref/met_html_focus.asp

0
source

Use this code: <textarea rows="20" cols="100">

Your HTML code: <textarea>

-2
source

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


All Articles