Nifty Modal - how to trigger a modal without a button

10,695

Solution 1

you can add the class "md-show" to the div element that you are using as the modal dialog

div example:

<div class="md-modal colored-header" id="div_modal">
    <div class="md-content">
        <div class="modal-header">
        </div>
        <div class="modal-body form">
        </div>
        <div class="modal-footer">
        </div>
     </div>
</div>
<div class="md-overlay"></div>

if you are using jQuery

query the div element by id to dynamically add the class

$("#div_modal").addClass("md-show");

To close the modal just remove that class

$("#div_modal").removeClass("md-show");

I hope that helps

Solution 2

Using the addClass and removeClass didn't give me the best results. I can't find the source, but I'm using a jQuery version of Nifty Modal that allows calls to the show and hide methods explicitly.

$("#div-modal").niftyModal("show");
$("#div-modal").niftyModal("hide");

Note: I'm not the author of the file: jquery.modalEffects.js:

//Button Trigger
(function ($) {
    var $this = new Object();
    var methods = {
        init: function (options) {
            $this = $.extend({}, this, methods);
            $this.searching = false;
            $this.o = new Object();

            var defaultOptions = {
                overlaySelector: '.md-overlay',
                closeSelector: '.md-close',
                classAddAfterOpen: 'md-show',
                modalAttr: 'data-modal',
                perspectiveClass: 'md-perspective',
                perspectiveSetClass: 'md-setperspective',
                afterOpen: function (button, modal) {
                    //do your stuff
                },
                afterClose: function (button, modal) {
                    //do your suff
                }
            };

            $this.o = $.extend({}, defaultOptions, options);
            $this.n = new Object();


            var overlay = $($this.o.overlaySelector);
            $(this).click(function () {
                var modal = $('#' + $(this).attr($this.o.modalAttr)),
                    close = $($this.o.closeSelector, modal);
                var el = $(this);
                $(modal).addClass($this.o.classAddAfterOpen);
                /* overlay.removeEventListener( 'click', removeModalHandler );
                overlay.addEventListener( 'click', removeModalHandler ); */
                $(overlay).on('click', function () {
                    removeModalHandler();
                    $this.afterClose(el, modal);
                    $(overlay).off('click');
                });
                if ($(el).hasClass($this.o.perspectiveSetClass)) {
                    setTimeout(function () {
                        $(document.documentElement).addClass($this.o.perspectiveClass);
                    }, 25);
                }

                $this.afterOpen(el, modal);
                setTimeout(function () {
                    modal.css({ 'perspective': 'none' });

                    //3D Blur Bug Fix
                    if (modal.height() % 2 != 0) { modal.css({ 'height': modal.height() + 1 }); }

                }, 500);

                function removeModal(hasPerspective) {
                    $(modal).removeClass($this.o.classAddAfterOpen);
                    modal.css({ 'perspective': '1300px' });
                    if (hasPerspective) {
                        $(document.documentElement).removeClass($this.o.perspectiveClass);
                    }
                }

                function removeModalHandler() {
                    removeModal($(el).hasClass($this.o.perspectiveSetClass));
                }

                $(close).on('click', function (ev) {
                    ev.stopPropagation();
                    removeModalHandler();
                    $this.afterClose(el, modal);
                });

            });

        },
        afterOpen: function (button, modal) {
            $this.o.afterOpen(button, modal);
        },
        afterClose: function (button, modal) {
            $this.o.afterClose(button, modal);
        }
    };

    $.fn.modalEffects = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.modalEffects');
        }

    };
    function is_touch_device() {
        return !!("ontouchstart" in window) ? 1 : 0;
    }
})(jQuery);

//Nifty Modal
; (function ($) {

    $.fn.niftyModal = function (method) {

        var defaults = {
            overlaySelector: '.md-overlay',
            closeSelector: '.md-close',
            classAddAfterOpen: 'md-show',
            modalAttr: 'data-modal',
            perspectiveClass: 'md-perspective',
            perspectiveSetClass: 'md-setperspective',
            afterOpen: function (modal) {
                //do your stuff
            },
            afterClose: function (modal) {
                //do your suff
            }
        }

        var config = {}

        var methods = {

            init: function (options) {
                return this.each(function () {
                    config = $.extend({}, defaults, options);
                    var modal = $(this);

                    //Show modal
                    helpers.showModal(modal);

                });
            },

            toggle: function (options) {
                return this.each(function () {
                    config = $.extend({}, defaults, options);
                    var modal = $(this);
                    if (modal.hasClass(config.classAddAfterOpen)) {
                        helpers.removeModal(modal);
                    } else {
                        helpers.showModal(modal);
                    }
                });
            },

            show: function (options) {
                config = $.extend({}, defaults, options);
                return this.each(function () {
                    helpers.showModal($(this));
                });
            },

            hide: function (options) {
                config = $.extend({}, defaults, options);
                return this.each(function () {
                    helpers.removeModal($(this));
                });
            }
        }

        var helpers = {

            removeModal: function (mod) {
                mod.removeClass(config.classAddAfterOpen);
                $(document.documentElement).removeClass(config.perspectiveClass);
                mod.css({ 'perspective': '1300px' });
                mod.trigger('hide');
            },

            showModal: function (mod) {
                var overlay = $(config.overlaySelector);
                var close = $(config.closeSelector, mod);
                mod.addClass(config.classAddAfterOpen);

                //Overlay Click Event
                overlay.on('click', function (e) {
                    var after = config.afterClose(mod, e);
                    if (after === undefined || after != false) {
                        helpers.removeModal(mod);
                        overlay.off('click');
                    }
                });

                //Fire after open event
                config.afterOpen(mod);
                setTimeout(function () {
                    mod.css({ 'perspective': 'none' });

                    //3D Blur Bug Fix
                    if(mod.height() % 2 != 0){mod.css({'height':mod.height() + 1});}

                }, 500);

                //Close Event
                close.on('click', function (ev) {
                    var after = config.afterClose(mod, ev);
                    if (after === undefined || after != false) {
                        helpers.removeModal(mod);
                        overlay.off('click');
                    }
                    ev.stopPropagation();
                });

                mod.trigger('show');
            }
        }

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method "' + method + '" does not exist in niftyModal plugin!');
        }

    }

})(jQuery);

Solution 3

Simply call $("#your element").niftyModal("show");

Solution 4

<!-- Hidden Button Modal -->
    <button style="display:none" data-toggle="modal" data-target="#mod-danger" id="sendClick" type="button" class="btn btn-danger">Danger</button>
<!-- Show  Button Modal -->
  <div id="mod-danger" tabindex="-1" role="dialog" class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" data-dismiss="modal" aria-hidden="true" class="close"><span class="s7-close"></span></button>
          </div>
          <div class="modal-body">
            <div class="text-center">
              <div class="text-danger"><span class="modal-main-icon s7-close-circle"></span></div>
              <h3 class="mb-4">Danger!</h3>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>Fusce ultrices euismod lobortis.</p>
              <div class="mt-6">
                <button type="button" data-dismiss="modal" class="btn btn-sm btn-space btn-secondary">Cancel</button>
                <button type="button" data-dismiss="modal" class="btn btn-sm btn-space btn-danger">Proceed</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

<!-- Call model in javascri ==  $("#sendClick").click(); -->

$("#sendClick").click();

Hi !

Share:
10,695
jwknz
Author by

jwknz

Hi, Working as a CS Tutor and enjoying full stack development in my day-to-day coding. Cheers, Jeff

Updated on June 04, 2022

Comments

  • jwknz
    jwknz almost 2 years

    This script provides awesome modal transitions and I want to use them instead of a standard alert message.

    Now the demo of the script shows how to trigger them by pressing a <button>, but I am having a little trouble finding how to display a modal as a result of an if statement for example.

    This is the code that is linked to the button.

    //This part is in an other file which is imported into the html file where the button is.

    $(document).ready(function(){
        $(".md-trigger").modalEffects();
    }); 
    

    //The function is triggered when a button has the class, but I need it work class independent, or at least that it can be triggered with it being linked to a button

    <button data-modal="modal-1" data-toggle="modal" data-target="#mod-warning" type="button" class="md-trigger btn btn-warning btn-flat"><i class="fa fa-warning"></i> Warning</button>
    

    Question: How do I trigger the nifty modal as a result of an if statement?