1

Is it possible something like this:

$('#btna').click(function(){
    console.log(abc());
})

function abc() {
    $('#btnb').click(function(){
        var result = false;
    });
    $('#btnc').click(function(){
        var result = true;
    });
    return result;
}

So clicking on btna I need to wait while btnb or btnc is clicked and write that result in console.

Any help?

1
  • Do you want to wait for both b and c to be clicked, or just one or the other? Based on your question, it sounds like the latter, but based on the answer you accepted, it sounds like the former. Commented Jun 20, 2018 at 22:47

2 Answers 2

2

Other Way to do, its using Promises, this will run in especific order waiting for previous click, except the first button ($btna)

const $btna = document.getElementById('btna');
const $btnb = document.getElementById('btnb');
const $btnc = document.getElementById('btnc');


function executeListener(btn ){
  return new Promise(function(resolve, reject) {
    btn.addEventListener("click" , function(){
      let jsonReturn = {
        action : "click" ,
        button : btn
      }
      resolve(jsonReturn)
    });
  })
}
executeListener($btna)
.then(function(btnaSuccess) {
  console.log("click btna");
  return executeListener($btnb)
}).then(function(btnbSuccess){
  console.log("click btnb");
  return executeListener($btnc);
}).then(function(btncSuccess){
  console.log("click btnc")
  console.log("Chispas");
})
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>

Also, you could use Promise.all

const $btna = document.getElementById('btna');
const $btnb = document.getElementById('btnb');
const $btnc = document.getElementById('btnc');


function executeListener(btn ){
  return new Promise(function(resolve, reject) {
    btn.addEventListener("click" , function(){
      let jsonReturn = {
        action : "click" ,
        button : btn
      }
      resolve(jsonReturn)
    });
  })
}
Promise.all([
executeListener($btna), 
executeListener($btnb),
executeListener($btnc)])
.then(values => { 
  console.log("resolve all clicks");  
  console.log("Chispas")
  
  

});
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>

Sign up to request clarification or add additional context in comments.

2 Comments

It works, thanks a lot. I just want to know is this possible using jquery, in order to code less. Anyway - accepted.
You didn't write that , but I'm happy for you, anyway , I prefer native JavaScript because if I have to change the library I will not have problems switching to another library;
1

Assuming you want to wait for all the buttons to be clicked (which seems to be the case given the answer you accepted), and assuming you are using ECMA 2017, you can use the more modern async/await pattern.

const btna = $("#btna");
const btnb = $("#btnb");
const btnc = $("#btnc");

async function handler(btn) {
   return new Promise((resolve) => {
     btn.on("click", () => {
        resolve(`clicked ${btn.attr("id")}`);
     });
   });
}

(async () => { 
  let values = await Promise.all([handler(btna), handler(btnb), handler(btnc)]);
  console.log(values); 
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>

Based on your question though, it sounds like you don't want to wait until all buttons are clicked. You just want to log different values when btnb is clicked after btna vs btnc is clicked after btna. In that case, you don't need promises at all and can just do this:

const btna = $("#btna");
const btnb = $("#btnb");
const btnc = $("#btnc");

btna.click(() => {
    btnb.on("click", clickHandler);
    btnc.on("click", clickHandler);
});

function clickHandler() {
  console.log(this.id);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>

1 Comment

more info async/await

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.