How do I stop and restart a JQuery function that can be triggered by multiple elements?

0

Hello,

I made a screen with a virtual keyboard, for a touch screen application. The client wants two inputs on the same screen and when the user selects an input, the keyboard must enter the values entered in the same input. I have the function working, but the problem is that every time I select an input, multiple instances of the function continue to run.

For example: If you tap 3 times on input 1 and once on input 2 and then type "Q" on the keyboard, it is inserted into input 1 "QQQ" and input 2 "Q" all at the same time. How can I stop and restart the function each time an input element is selected?

The function name is termoConsulta(param) . I'm using click() method to select the input.

$('#input1').click(function(){
    termoConsulta('#input1');
});
$('#input2').click(function(){
    termoConsulta('#input2');
});

function termoConsulta($escreveInput){

        $('#teclado li').click(function(){
            if(!$(this).is("#consultar")) {
                var $this = $(this),
                    digito = $this.html();

                if($this.hasClass('tecla-x')){
                    var entrada = $escreveInput.val();
                    //apaga último dígito
                    $escreveInput.val(entrada.substr(0,entrada.length-1));
                    return false;
                }

                //escreve o dígito no input
                $escreveInput.val($escreveInput.val()+digito);
            }
        });

        //apaga TUDO
        $('#apagar').click(function(){
            $escreveInput.val('');
        });     
}
    
asked by anonymous 19.12.2017 / 17:00

0 answers