focus () of JQuery in Firefox

1

Good morning,

I have a problem with firefox. I have a method that clears an input with a value that I consider invalid there in the sequence I give focus to that input. In Internet Explorer and Chrome it works.

The methods I use is this:

$(".field-date").blur(function () {
    if (!DtValida(this.value)) {
        MsgAlerta("Data inválida.");           
        this.value = "";
        this.focus();
        return false;
    }
    return true;       
});

In Firefox it clears the input and shows the message, only the focus does not work.

    
asked by anonymous 30.11.2016 / 13:51

2 answers

2

First, good morning Kaique. From what I've researched here is a workaround for this issue . Try to wait for the next tick to be able to perform the focus.

setTimeout(function() {
        $(this).focus();
    }, 0);

UPDATE

It is worth remembering that within the function setTimeout() $(this) has another scope, so it is necessary to focus on the selected element.

To do this, just put the element exactly:

setTimeout(function() {
            $(".field-date").focus();
        }, 0);

I hope I have helped.

    
30.11.2016 / 14:00
0

It worked like this:

classe.blur(function () {
    var componente = $(this);

    if (!DtValida(this.value)) {
        MsgAlerta("Data inválida.");
        componente.val("");
        setTimeout(function () {
            componente.focus();
        });
        return false;
    }
    return true;
});

I had tried with setTimeout previously and it did not work there, I took the last parameter of the interval that worked, I do not quite understand what this changes I'm going to research better.

Using the selector with the class caused me a problem because I have more than one element with this class, so the focus always went to the last one. Then I searched the selector again instead of using this direct thanks to the scope tip in Andrew's update.

Thanks for the help Andrew.

    
30.11.2016 / 23:46