Varda Elentári
Varda Elentári

Reputation: 2322

How to prevent Bootbox from displaying twice after a double click?

I have a <ul> element that opens a bootbox when it's clicked. Double clicking this element triggers the onclick in JQuery twice

$("#email-list").on("click", ".list-group-item", function (e) {
bootbox.confirm("Send a forgotten password email to " + email + "?", function (result) {...}}

I tried using 'e.preventDefault()'

$(document).ready(function () {

    $("#email-list").dblclick(function (e) {

        e.preventDefault();
    });

});

I even tried disabling clicking on the element but both failed. The bootbox still appears twice.

$("#email-list").bind('click', function () { return false; });
//...do stuff
$("#email-list").unbind('click');

Anyone has a suggestion?

Upvotes: 6

Views: 4754

Answers (4)

Varda Elent&#225;ri
Varda Elent&#225;ri

Reputation: 2322

Another solution can be to add:

bootbox.hideAll();

to hide any other bootboxes right before showing the bootbox like so:

bootbox.hideAll();
bootbox.confirm("Some Message " , function (result){/*do stuff*/}

Upvotes: 8

Varda Elent&#225;ri
Varda Elent&#225;ri

Reputation: 2322

I figured the best way to do this is to separate the two events; onclick and dbclick, I used something like this, I hope it will save someone some time:

var DELAY = 700, clicks = 0, timer = null;

    $(function () {

        $("#email-list").on("click", ".list-group-item", function (e) {

            clicks++;  //count clicks

            if (clicks == 1) {
                   //do stuff
                    clicks = 0;  //after action performed, reset counter

                }, DELAY);
            } else {

                clearTimeout(timer);    //prevent single-click action
                clicks = 0;             //after action performed, reset counter
                return false;
            }

        })
        .on("dblclick", function (e) {
            e.preventDefault();  //cancel system double-click event
        });
   }

Upvotes: 0

simdrouin
simdrouin

Reputation: 1018

Use the click event, then you can replace

e.preventDefault(); 

with

e.stopPropagation(); 

or

return false;

Upvotes: 0

Jasper Giscombe
Jasper Giscombe

Reputation: 313

Try this:

$("#email-list").on("click", ".list-group-item", function (e) {
   if(!$('#myModal').is(':visible')){
       $('#myModal').modal('show');  
   }
   e.preventDefault();
}

Upvotes: 1

Related Questions