user1613826
user1613826

Reputation: 1

jQuery create input field and set focus

I'm trying to:

The new list item gets created, but no focus. Help is much appreciated!

Upvotes: 0

Views: 3681

Answers (3)

VisioN
VisioN

Reputation: 145408

There is no handler as a second parameter of before, so just use:

$("#newList").click(function() {
    $('li.divider').before('<li class="newListName"><input id="newListNameInput" /></li>');
    $('#newListNameInput').focus();
});

But be careful with duplication of IDs if you add many new input elements. Maybe it is better to use classes.

DEMO: http://jsfiddle.net/CANWE/

Upvotes: 0

Kamal
Kamal

Reputation: 2180

try this http://jsfiddle.net/CWqxk/

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262979

before() is synchronous and does not take a callback argument. You only have to write:

$("#newList").click(function() {
    $('li.divider').before('<li class="newListName"><input id="newListNameInput" /></li>');
    $('#newListNameInput').focus();
});

Or maybe, if you want to take advantage of chaining:

$("#newList").click(function() {
    $('<li class="newListName"><input id="newListNameInput" /></li>')
        .insertBefore("li.divider").find("input").focus();
});

Upvotes: 1

Related Questions