Reputation: 1
I'm trying to:
set the focus to that new field
$("#newList").click(function () {
$('li.divider').before('<li class="newListName"><input id="newListNameInput" /></li>',function() {
$('#newListNameInput').focus();
});
});
The new list item gets created, but no focus. Help is much appreciated!
Upvotes: 0
Views: 3681
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
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