Question

I was wondering how I can trigger an event after two different buttons being clicked. Specifically I have some buttons with different ids and I want when I click two specific buttons from them to trigger an event. I thought that this can be happen If I call some function in the first on click event and inside this function I can call another function if the second button being clicked. It does not seem to work, so I need your help. (If anyone needs more code please comment to upload the whole code)

     function someKindOfFunction(){  
         //there is some other code here

         var ob = document.getElementById(idTable[0]); 
         var ob1 = document.getElementById(idTable[1]); 
         var ob2 = document.getElementById(idTable[2]); 
         var ob3 = document.getElementById(idTable[3]); 


         ob.addEventListener("click",function() {onCl1(idTable[0],idTable)});
         ob1.addEventListener("click",function() {onCl1(idTable[1],idTable)});
         ob2.addEventListener("click",function() {onCl1(idTable[2],idTable)});
         ob3.addEventListener("click",function() {onCl1(idTable[3],idTable)});


    }
    
    function onCl1(id1,idTable){
        var obj1 = document.getElementById(id1);
        obj1.disabled=true;
        var obj2,v,obj3,obj4;
        v=0;
        var temp = ["0","0","0"];
        for(var i =0 ; i<4 ; i++){
          if( id1 != idTable[i]){
            temp[v] = idTable[i];
            v=v+1;
          }
        }
        ob=document.getElementById(temp[0]);
        ob1=document.getElementById(temp[1]);
        ob2=document.getElementById(temp[2]);

        ob.addEventListener("click",function() {onCl2(id1,temp[0])});
        ob1.addEventListener("click",function() {onCl2(id1,temp[1])});
        ob2.addEventListener("click",function() {onCl2(id1,temp[2])});
        
        
      }
      
      function onCl2(id1,id2){
          //some kind of actions
          alert("it wokrs");

      }
Was it helpful?

Solution

Just pass some sort of value whenever the button you want is clicked. For instance:

 ob.addEventListener("click",function() {onCl1(1)});
 ob1.addEventListener("click",function() {onCl1(1)});
  var i;   
function onCl1(value){
i += value;
if(i == 2){
//do this
   }
}

So basically once these two buttons are clicked the value will equal 2 and trigger whatever you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top