Domanda

After appending input box to other element, it would not let to click in the box (the focus goes away instantly). Why would this happen? Here is JSFIDDLE

HTML

<div id="placeHolder" onclick="appendSearch()">placeHolder</div>
<input id='box' type='text'></input>

JS

function appendSearch(){
    $("#box").appendTo($("#placeHolder"))
}
È stato utile?

Soluzione 2

The appendTo method is what is causing the input to lose focus, as the input is now in the div, and it is calling the function on click. The following code will account for that.

function appendSearch(){
    if ($("#placeHolder input").length == 0) {
        $("#box").appendTo($("#placeHolder"));
    }
}

Altri suggerimenti

If you want to put focus directly in your text box use .focus() :

$("#box").appendTo($("#placeHolder")).focus();

I updated your jsFiddle

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top