Search code examples
jquerysimplemodal

JQuery SimpleModal - internal links?


I managed to get multiple modal windows working using the simplemodal plugin, but I need to be able to switch between them without having the user manually close it first.

At this point I'm still learning this by example. I have seen a couple other references to this problem, but the offered solutions either don't work or don't start with the same building blocks.

Any tips or advice greatly appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SimpleModal</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type='text/javascript' src='js/jquery.simplemodal.js'></script>
<script>
jQuery(function ($) {
    $('#basic-modal .link1').click(function (e) {
        $('#link1-modal-content').modal();
        return false;
    });

    $('#basic-modal .link2').click(function (e) {
        $('#link2-modal-content').modal();
        return false;
    });

    $('#basic-modal .link3').click(function (e) {
        $('#link3-modal-content').modal();
        return false;
    });
});
</script>
<style>
#simplemodal-overlay {background-color:#000; cursor:wait;}
</style>
</head>

<body>

<!-- MAIN PAGE / LINKS TO MODAL WINDOWS-->
<div id='basic-modal'>
    <a href="#" class="link1" id="pop1">Link to Content 1</a><br />
    <a href="#" class="link2" id="pop2">Link to Content 2</a><br />
    <a href="#" class="link3" id="pop3">Link to Content 3</a><br />
</div>



<!-- INDIVIDUAL MODAL WINDOWS CONTENT  -->
<div id="link1-modal-content" style="display:none; width:200px; height:200px; background-color:#FFFFFF;">
    <p>Content 1</p>
    Content 1<br />
    <a href="#" class="link2" id="pop2">Link to Content 2</a><br />
    <a href="#" class="link3" id="pop3">Link to Content 3</a><br />
</div>

<div id="link2-modal-content" style="display:none; width:200px; height:200px; background-color:#FFFFFF;">
    <p>Content 2</p>
    <a href="#" class="link1" id="pop1">Link to Content 1</a><br />
    Content 2<br />
    <a href="#" class="link3" id="pop3">Link to Content 3</a><br />
</div>

<div id="link3-modal-content" style="display:none; width:200px; height:200px; background-color:#FFFFFF;">
    <p>Content 3</p>
    <a href="#" class="link1" id="pop1">Link to Content 1</a><br />
    <a href="#" class="link2" id="pop2">Link to Content 2</a><br />
    Content 3<br />
</div>


</body>
</html>

Solution

  • SimpleModal only allows for one modal to be open at any point, as such you need to close any existing modal and open a new one or swap their content.

    To open/close use the following in place of your javascript :

    function closeAllModal() {
        $.modal.close();
    }
    
    jQuery(function ($) {
    
    
        $('.link1').click(function (e) {
            closeAllModal();
    
            $('#link1-modal-content').modal();
            return false;
        });
    
    
        $('.link2').click(function (e) {
            closeAllModal();
    
            $('#link2-modal-content').modal();
            return false;
        });
    
        $('.link3').click(function (e) {
            closeAllModal();
            $('#link3-modal-content').modal();
    
            return false;
        });
    });
    

    I have altered the click selector to be generic so it binds to all the links and added a call to close the modals before opening in each. This should give you the desired effect.