Finding the last focused element with jQuery.

Posted by Joshua Cody on Stack Overflow See other posts from Stack Overflow or by Joshua Cody
Published on 2010-06-02T00:41:04Z Indexed on 2010/06/02 0:43 UTC
Read the original article Hit count: 531

Filed under:

I'm looking to determine which element had the last focus in a series of inputs, that are added dynamically by the user. This code can only get the inputs that are available on page load:

$('input.item').focus(function(){
    $(this).siblings('ul').slideDown();
});

And this code sees all elements that have ever had focus:

$('input.item').live('focus', function(){
    $(this).siblings('ul').slideDown();
});

The HTML structure is this:

<ul>
    <li><input class="item" name="goals[]">
    <ul>
        <li>long list here</li>
        <li>long list here</li>
        <li>long list here</li>
    </ul></li>
</ul>
<a href="#" id="add">Add another</a>

On page load, a single input loads. Then with each add another, a new copy of the top unordered list's contents are made and appended. And when each gets focus, I'd like to show the list beneath it. But I don't seem to be able to "watch for the most recently focused element, which exists now or in the future."

Do I have some sort of fundamental assumption wrong?

© Stack Overflow or respective owner

Related posts about jQuery