HTMLGuyLLC / jAlert

jQuery alert/modal/lightbox plugin
https://htmlguyllc.github.io/jAlert/
MIT License
76 stars 35 forks source link

'Enter' and 'Spacebar' keys fires the confirm alert again #40

Closed r2edwin closed 7 years ago

r2edwin commented 7 years ago

Hi. I' m using jAlert-functions.js wich redefines the -alert- native function. But if I hit Enter or Spacebar when the alert is on screen, it fires the alert again and I need to press Escape key many times to close them. I' ve made a few modification to the jAlert.js and could solve the "Enter key problem". This are my modifications:

Line 157 approximately: closeAlert: function(remove, onClose) { if( remove != false ) { remove = true; } / add this / $(document).off('keydown', $.fn.jAlert.onEnter);

Line 277 if( alert.video && alert.video.indexOf('youtube.com/watch?v=') > -1 && alert.video.indexOf('embed') === -1 ) { alert.video = alert.video.replace('watch?v=', 'embed/'); } / add this / if( alert.type == 'modal' ) $(document).on('keydown', $.fn.jAlert.onEnter);

if( alert.type == 'confirm' ){
      .........rest of original code here

Line 860: / Keydown on document (escape key) / $.fn.jAlert.onEscKeyDown = function(e){ ,,,,original code here }

/* Keydown on document (enter key) */
$.fn.jAlert.onEnter = function(e){
    /* Enter = 13 */
    if(e.keyCode === 13) {
        e.preventDefault();
        var lastVisibleAlert = $('.jAlert:visible:last');
        if( lastVisibleAlert.length > 0 )
            lastVisibleAlert.jAlert().closeAlert();
    }
};

i've tried to do the same with the spacebar key but I could not do it. Any ideas ? Thanks in advance

HTMLGuyLLC commented 7 years ago

You're looking at the wrong end of things. The issue is because the user's focus is still on the button or element that was clicked to create the popup. You just need to focus on something else or blur the focus. I thought I had added that to the code already, but it's really simple for you to do. Right after you popup the alert...try $('body').focus(); or something like that.

HTMLGuyLLC commented 7 years ago

Or try $('.your-btn-element').blur();