Why focus event is triggered 2 times?

2

I have an INPUT, I try to automatically register when the focus is on it. By making focus sent a message to ask if you are sure to record the current date (could be a mistake finger) and here is my problem, because the message is repeated 2 times and do not understand why. The message I show with the JQUERYCONFIRM plugin.

$('.fecha').on('focus',function() {

    var hoy = new Date();
    var dia = hoy.getDate();
    var mes = hoy.getMonth()+1;
    var año = hoy.getFullYear();

    if(dia<10) {dia='0'+dia;} 
    if(mes<10) {mes='0'+mes;}    

    hoy = año + '-' + mes + '-' + dia;

    //$(this).val(hoy);

    $.confirm({
        theme: 'material',
        title: 'Atencion',
        content: '¿Ingresar Fecha?',
        useBootstrap: false,
        boxWidth:'300px',
        buttons:{
            Adelante: function(){ ingresarFecha() },
            Cancelar: function(){}
        }
    });

});
    
asked by Oscar Díaz 10.04.2017 в 02:14
source

1 answer

0

If you want to make sure that this function is executed only once when the event occurs, use unbind() , with your code it would be:

$('.fecha').unbind().on('focus',function() {

        var hoy = new Date();
        var dia = hoy.getDate();
        var mes = hoy.getMonth()+1;
        var año = hoy.getFullYear();

        if(dia<10) {dia='0'+dia;} 
        if(mes<10) {mes='0'+mes;}    

        hoy = año + '-' + mes + '-' + dia;

        //$(this).val(hoy);

        $.confirm({
            theme: 'material',
            title: 'Atencion',
            content: '¿Ingresar Fecha?',
            useBootstrap: false,
            boxWidth:'300px',
            buttons:{
                Adelante: function(){ ingresarFecha() },
                Cancelar: function(){}
            }
        });

    });

However, you can use the unbind() function instead of off() .

I hope it helps you

    
answered by 10.04.2017 в 08:18