ive got this function and it works to select text on click
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);
}
}
using:
$('input').click(select_text);
but i would like to select the text when a textfield is focused (its focused with jquery when i hit enter in another textfield).
i have tried this:
$('input#id').focus(select_text);
but it didnt work. could someone tell me how to write for this to work.
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:
$("#settings_view #password #new_password").live('keyup', function(event) {
if(event.keyCode == 13)
{
$("#settings_view #password #password_beam").html('Enter password:<input id="password_confirm" type="password" value="0123456789" />');
$('input#password_confirm').focus(function (e) {
var element = this;
setTimeout(function () {
select_text.call(element, e);
}, 0);
});
}
});
i also tried this for live:
$("#settings_view #password #new_password").live('keyup', function(event) {
if(event.keyCode == 13)
{
$("#settings_view #password #password_beam").html('Enter password:<input id="password_confirm" type="password" value="0123456789" />');
$('input#password_confirm').live('focus', function (e) {
var element = this;
setTimeout(function () {
select_text.call(element, e);
}, 0);
});
}
});
but it didnt work as well. some ideas?