How to invoke function at the first click of the button and another at the second click

Asked

Viewed 1,326 times

1

How do I call the first function within the first click of the button, by event ondblclick(); and the second function at the second click of the same button.

I imagine invoking the function of the first click on the button and the other on the second click, like this:

note how it should be:

function a(){
   ...
declaração 
   ...
}
function b(){
   ...
declaração
   ...
}

Already on the button, something like:

<button ondblclick="a();?:b();">2-in-1</button>

It’s simple for those who know how to manipulate ?:, the same of if .. else, only that of tiny formula and appropriate to this idea.

Thanks from now on, help from all.

  • 2

    What do you mean, you ask and put the solution? That’s how it even, if it didn’t work, there must be another problem ....

  • I believe what he says is the first click a function, the second click a function. No ondbl if I’m not mistaken it makes the function (if you have more than one it does together) with 2 clicks, I’m wrong ?

1 answer

0


Man, I don’t know if this is the best practice, but it worked for me

Jquery

var cont = 0;
$( "#teste" ).click(function() {  
    if(cont == 0)
      {
     a();
      cont += 1;
      }
  else
      {
      b();
      cont = 0;
      }
});

function a(){
 alert('aqui');
}

function b(){
 alert('aqui 2');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a href="#" id="teste">
teste
</a>

Now transcription for:

Javascript

var cont = 0;

document.getElementById( "teste" ).onclick = function() {  
if(cont == 0)
{
a();
cont += 1;
}
else
{
b();
cont = 0;
}
}

function a(){
alert('função A');
}

function b(){
alert('função B');
}
<button id="teste">Clique-e-Descomplique</button>

Browser other questions tagged

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