jquery set focus on dynamic content?

Posted by Alan on Stack Overflow See other posts from Stack Overflow or by Alan
Published on 2010-05-17T09:25:09Z Indexed on 2010/05/17 9:30 UTC
Read the original article Hit count: 676

Filed under:
|
|

In jquery I've appended a <li> element to an unordered list.

How do I focus on the newly created <li> ?

If I do the following:

$("ul").append('<li><input type="text">hi!</input></li>');
$("li:last").focus(); //doesn't work because new <li> isn't in dom yet

the focus doesn't work, as noted above.

I know jquery 1.4.2 has a live() event handler which allows you load event handlers to dynamically added elements, but I'm not sure what I'm doing wrong:

$(document).ready(function () {

    $('li').live('load', function () {
       alert("hi!");
       $("li:last").focus();
    });
 });

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about focus