David542
David542

Reputation: 110093

Focus on textarea

How would I do the following in javascript:

<textarea onload="focus">Here is some text</textarea>

Basically, I want the textarea field to be focused on, and I want to do it within the html tag.

Upvotes: 3

Views: 1323

Answers (2)

pfkurtz
pfkurtz

Reputation: 514

You would need to provide some way to select your textarea, like an ID: <textarea id="my-textarea"></textarea>.

Then you can use the DOM API: document.getElementById('my-textarea').focus().

Or use jQuery: $('#my-textarea').focus().

Upvotes: 3

Joniras
Joniras

Reputation: 1338

Try out the autofocus attribute:

 <textarea autofocus>
At w3schools.com you will learn how to make websites.
</textarea> 

w3schools

Upvotes: 3

Related Questions