King Kong
King Kong

Reputation: 2915

Put element under focus in jquery

I want to put element under focus just after append it, means something similar to :

$("id").append($("elementid")).focus();        // not working

Upvotes: 1

Views: 125

Answers (3)

Sergei Zahharenko
Sergei Zahharenko

Reputation: 1524

Try trigger

$("#id").trigger('focus');

Upvotes: 1

KARASZI István
KARASZI István

Reputation: 31477

The jQuery ID selector needs a # before the id.

So

$("#id").append($("#elementid")).focus();

should work if you want to put focus on your #id DOM element, if you want to put focus on the #elementid DOM element, then you can use VisioN's answer.

Upvotes: 2

VisioN
VisioN

Reputation: 145458

In your example you are adding focus to id element, that's why it doesn't work.

Try this solution instead:

$("#elementid").appendTo("#id").focus();

DEMO: http://jsfiddle.net/wRSBY/

Upvotes: 3

Related Questions