Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a jQuery UI Dialog that gets displayed when specific elements are clicked. I would like to close the dialog if a click occurs anywhere other than on those triggering elements or the dialog itself.

JavaScript
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript" language="javascript">
        function deselect(e) {
            $('.pop').slideFadeToggle(function () {
                e.removeClass('selected');
            });
        }

        $(function () {
            $('#contact').on('click', function () {
                if ($(this).hasClass('selected')) {
                    deselect($(this));
                } else {
                    $(this).addClass('selected');
                    $('.pop').slideFadeToggle();
                }
                return false;
            });

            $('.close').on('click', function () {
                deselect($('#contact'));
                return false;
            });

        });

        $.fn.slideFadeToggle = function (easing, callback) {
            return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
        };
</script>
Posted
Updated 1-Feb-15 1:41am
v2

1 solution

your way looks ok, using hasClass or check for $(event.target).is(input) or something, where you'd check whatever you want / dont want to be clicked, before running the toggle command.

or hook up the escape button:
JavaScript
$(document).on('keyup',function(e){
    if(e.keyCode == 27){
      $('#dialog').slideFadeToggle();
    }
 });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900