Search code examples
javascriptjqueryjquery-uisimplemodal

Contents inside Popup Modal not showing up


I am using this Tutorial to show a Popup Modal Window on page load, For some reason it is not showing the contents & image inside the Modal.

Code i am working on is at jsFiddle

I want a simple Modal popup on which i can show Message which are in form of image in different dimension, I want Modal to show in the middle of the page and adjust width & height as per the image. I would appreciate help in this regard.

CODE

<!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>
<title>Simple JQuery Modal Window from Queness</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$(document).ready(function() {
        var id = $(this).attr('href');
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect    
        $('#mask').fadeIn(1000);   
        $('#mask').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });    

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });        

    $(window).resize(function () {

        var box = $('#boxes .window');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        box.css('top',  winH/2 - box.height()/2);
        box.css('left', winW/2 - box.width()/2);

    });    
});
</script>
<style>

/* Z-index of #mask must lower than #boxes .window */
#mask {
  position:absolute;
  z-index:9000;
  background-color:#000;
  display:none;
}
#boxes .window {
  position:fixed;

  display:none;
  z-index:9999;
  padding:20px;
}
/* Customize your modal window here, you can add background image too */
#boxes #dialog {

}
</style>

</head>
<body>

<div id="boxes">
    <!-- #customize your modal window here -->
    <div id="dialog" class="window">
        <b>Testing of Modal Window</b> |
        <!-- close button is defined as close class -->
        <a href="#" class="close">Close it</a>
 <br />
        <img src="http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg" />
    </div>
    <!-- Do not remove div#mask, because you'll need it to fill the whole screen -->
    <div id="mask"></div>
</div>

</body>
</html>

Solution

  • The error is this line op

        var id = $(this).attr('href');
    

    On document.ready you dont have a href attribute, but if you replace it to

        var id = "#dialog";
    

    Youre modal will work. See: http://jsfiddle.net/HJUZm/3/

    More...

    For the tutorial you are reading, I can see 2 possible mistakes.

    First is that in your HTML you are missing

        <!-- #dialog is the id of a DIV defined in the code below -->
        <a href="#dialog" name="modal">Simple Modal Window</a>
    

    And for some reason you have forgotten to use $('a[name=modal]').click(function(e) { and instead just have inserted it in document.ready

    The differences between $('a[name=modal]') is that document.ready is fired once when page is ready, and $('a[name=modal]').click(function(e){...}); is fired on every click where a name attribute is modal