I'm trying to recreate the native confirm component of javascript and I came across a question.
Example:
if (confirm("meu texto vai aqui")) { // Aguarda retorno {true ou false}
console.log("faça algo aqui");
}
By default, as long as the user does not click OK or Cancel, it will not return to the if stream. in this case I'm trying to reproduce it.
Example:
function msgConfirm(texto) {
var resposta = $(".resposta");
resposta.empty();
var modal =
"<div class='modal fade msgConfirm' tabindex='-1' role='dialog'>" +
" <div class='modal-dialog modal-sm' >" +
" <div class='modal-content' >" +
" <div class='modal-header' >" +
" <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'> × </span></button >" +
" <h4 class='modal-title' id='msgConfirmTitle'> </h4>" +
" </div>" +
" <div class='modal-body'>" +
" <p id='msgConfirmContent' > </p>" +
" </div>" +
" <div class='modal-footer' >" +
" <button type='button' class='btn btn-primary' id='msgConfirmConfirm' > Ok </button>" +
" <button type='button' class='btn btn-default' data-dismiss='modal' id='msgConfirmCancel' > Cancelar </button>" +
" </div>" +
" </div>" +
" </div>" +
"</div>";
resposta.append(modal);
resposta.find("#msgConfirmContent").html(texto);
resposta.find(".msgConfirm").modal("show");
$("#msgConfirmConfirm").click(function() {
return true;
});
}
And the moment I call it the return is given, without the intervention of the event listener.
if (msgConfirm("meu texto vai aqui")) { // Retorno imediato {undefined}
console.log("faça algo aqui");
}
So what do I need to know and how can I reproduce the behavior of the confirm?