How to make a function return result just after click?

1

When using the confirm() function in JavaScript, it usually looks like this:

var resultado = confirm("Deseja realmente confirmar?");
if(resultado){
    //confirmou...
}

I would like to know if there is any way to do the same thing with a function of its own, eg

var resultado = minhaCofirmacao("Deseja realmente confirmar?");
if(resultado){
    //...
}

Here's an example of the function (only with the practical parts of the question): FIDDLE

I tried doing this by creating a listener for when the user clicked on the confirmation button of my function, but it returns before the user triggers the event by itself.

Is there any way to create this type of return dependent on a user action without being callback ?

    
asked by anonymous 10.06.2014 / 23:58

1 answer

1

You can do everything through JQuery like this:

var div = $("<div />").addClass("popup").appendTo($("body"));

$("<button />").append("Confirmar?").appendTo(div).click(function() {
    var confirmou = confirm("Deseja realmente confirmar?");
    if (confirmou) {
        alert("Confirmou! =)");
    } else {
        alert("Não confirmou... =(");
    }
});

Note that I created a div with the popup class you created and then just added the button to that div. I hope it solves your problem.

    
20.06.2014 / 01:18