BASILIO
BASILIO

Reputation: 887

Set focus on appear

I have a page that has a lot of JS created elements. So i wish to focus a textarea after it appear. I try to make it with help of addEventListener like so:

mytextarea.addEventListener('someEvent', function(e)
    {
    this.focus();
    });

My problem is, that i can not found the right Event, that would be fired at the moment, when textarea get appended in the document, like here

document.getElementsByTagName('body')[0].appendChild(mytextarea);

Native JS Only please. Thank you

Upvotes: 0

Views: 499

Answers (3)

Ahmed Assaf
Ahmed Assaf

Reputation: 601

Put your focus in

$(document).ready(function(){
//Here
});

This mean your textarea is appended to the document.

And without jQuery :

window.addEventListener('load', function () {
//here
});

Upvotes: 0

palaѕн
palaѕн

Reputation: 73906

Have you tried this:

document.getElementsByTagName('body')[0].appendChild(mytextarea);
mytextarea.focus();

FIDDLE

Upvotes: 1

Dziad Borowy
Dziad Borowy

Reputation: 12579

this should work:

<textarea autofocus></textarea>

Upvotes: 0

Related Questions