2
I need when the field gets the focus, the cursor to be in the last character. The way the cursor is at the beginning of the field even if the field is not empty.
function gravaObs(unidadeObs) {
var gasObs = $('#obsGas_'+unidadeObs).val();
swal({
title: "Inserir Observações",
html: '<input type="text" id="obsCampo'+unidadeObs+'" class="col-xs-12 form-control" value="'+gasObs+'" /><br>',
confirmButtonText: "Salvar",
confirmButtonColor: "#5cb85c",
cancelButtonText: "Cancelar",
cancelButtonColor: "#d9534f",
showCancelButton: true,
onOpen: function () {
$('#obsCampo'+unidadeObs).focus();
$('#obsCampo'+unidadeObs).on('focus', function() {
var pos = this.value.length * 2;
this.setSelectionRange(pos, pos);
});
}
}).then(function(){
var resultado = $('#obsCampo'+unidadeObs).val();
console.log(resultado);
$('#obsGas_'+unidadeObs).val(resultado);
}).catch(swal.noop);
}
Using boot it works but as the focus arrives in the field through a function did not work.
– Ezequiel Tavares
@Ezequieltavares must be doing something wrong. Take a look at the answer now with a function example after 3 seconds.
– Sergio
I edited the question with the full code already with the changes I tested.
– Ezequiel Tavares
as is it gets the focus through a function
– Ezequiel Tavares
@Ezequieltavares with more code now I understand your problem better. Two things:
1-
Are you calling the.focus()
before the event headset: it has to be the other way around.2-
As this input is created dynamically you have to use delegation. Example: http://jsfiddle.net/Sergio_fiddle/e4kv80pm/– Sergio
perfect Die... worked round including in IE. hug
– Ezequiel Tavares