How to stop and restart a Jquery function that can be triggered by several elements?

Asked

Viewed 96 times

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 typed in that same input. I have the function working, but the problem is that each time I select an input, multiple instances of the function continue running.

For example: If I tap input 1 3 times and once 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 method click() 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('');
        });     
}
  • Enter the code of the thermoConsult function. already tried to associate the event to the function call?

  • @Davidvinicius added the code. How can I associate to the call? Sorry ignorance, I’m learning to program yet.

  • You mean in the HTML file, with Onclick()?

  • It’s like this, try using Onclick.

  • I ended up removing the function. Now there is simply a command in the code, and every time an element is clicked, the instructions are called. It works like Onclick. Thank you @Davidvinicius

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.