Frank Akers Frank Akers - 4 months ago 21
Javascript Question

JQuery Skipping click inside function

My problem is fairly simple: My code will start with an alert, and afterwards, should wait for the user to click a button in order to go on with the other alerts: (JSFiddle Here)

function clicker(){
alert ("Step 1.9: Clearly it is entering func");
$("#button").click(function(){
alert("Step 2 ALERT"); //SKIPS THIS PART
})
}

$(document).ready(function(){
alert("hello, step 1");

if (1==1) {
clicker();
alert("Step 3:Done with 'if' ");
}
alert("step 4 DONE with whole code");
})


As you can see, the problem is that it keeps skipping alert #2. How should I write this code so that jQuery 'waits' for a click before moving on?

Thanks.

Answer

You have to take the 'clicking' function out of the clicker() function and put it in the if:

$(document).ready(function(){
    if(1=1){
        ("#button").click(function(){
            clicker();
            //Rest of alerts HAVE to go in here
        })
    }
})

Sorry, but there is no other way to pause jQuery code.