I want to write a function that: (1) automatically create textfield within an HTML element (2) autofocus on the new created textfield
The HTML looks like this: <span id="dummy" onClick="createAndFocus(this)">some content</span> Here is my code. This can create the textfield. But it cannot autofocus on the textfield: function createAndFocus(item) { id = $(item).attr('id'); value = $(item).text(); $(item).html('<input class="todo-textfield" value="'+value+'" '+ ' onBlur="javascript:textfieldToSpan(\''+id+'\', this.value)" />' ).attr("extra", "done").ready(function() { $("#"+id+" .todo-textfield").focus(); // failed here }); } What is wrong?