select text on focus
<p>ive got this function and it works to select text on click</p>

<pre><code>function select_text(ev) {
    if (this.createTextRange)
    {
        // This is for IE and Opera.
        range = this.createTextRange();
        range.moveEnd('character', this.value.length);
        range.select();
    }
    else if (this.setSelectionRange)
    {
        // This is for Mozilla and WebKit.
        this.setSelectionRange(0, this.value.length);
    }
}
</code></pre>

<p>using:</p>

<pre><code>$('input').click(select_text);
</code></pre>

<p>but i would like to select the text when a textfield is focused (its focused with jquery when i hit enter in another textfield).</p>

<p>i have tried this:</p>

<pre><code>$('input#id').focus(select_text);
</code></pre>

<p>but it didnt work. could someone tell me how to write for this to work.</p>

<p>EDIT: the element that is going to be focused and selected is added with ajax! forgot to tell you that. ive tried this one but it didnt work:</p>

<pre><code>$("#settings_view #password #new_password").live('keyup', function(event) {
    if(event.keyCode == 13)
    {
        $("#settings_view #password #password_beam").html('Enter password:&lt;input id="password_confirm" type="password" value="0123456789" /&gt;');
        $('input#password_confirm').focus(function (e) {
            var element = this;
            setTimeout(function () {
                select_text.call(element, e);
            }, 0);
        });
    }
});
</code></pre>

<p>i also tried this for live:</p>

<pre><code>$("#settings_view #password #new_password").live('keyup', function(event) {
    if(event.keyCode == 13)
    {
        $("#settings_view #password #password_beam").html('Enter password:&lt;input id="password_confirm" type="password" value="0123456789" /&gt;');
        $('input#password_confirm').live('focus', function (e) {
            var element = this;
            setTimeout(function () {
                select_text.call(element, e);
            }, 0);
        });
    }
});
</code></pre>

<p>but it didnt work as well. some ideas?</p>
