Skip to main content

Mootools Basics - A Series

By
Published October 5, 2009

How can you leverage a JavaScript framework to create a more exciting and dynamic community? follow this series to learn how.

Episode 2: Creating your first Modal Box

Basically, a modal box is a window that requires some type of input from the user before they can continue (for example, click here to create a modal box). This can be useful for displaying important information (for example, if a user remains inactive for too long, a warning box will appear on your page warning you that your session will end.), or for entering information (for example, when sending a user a message, a modal box will popup, allowing you to send a message without leaving the page).

In our last part (click here to view the article), we learned how to find an element in a document, and how to manipulate it. In our example, we changed the text to be something else. To create a modal box, you will have to specify the element what will become the modal box, and what element will trigger the modal box. in the last article, we specified that a link object will be the trigger, and a div object will hold the HTML for the box. In this example, our HTML would look like this:

<a href="#" id="example-modal-box-trigger">Click Here for a modal box</a>
<div id="example-modal-box-content" class="hide">
    Here is the content
</div>

This gives us the framework to create the modal box. Our "example-modal-box-trigger" will cause "example-modal-box-content" to display when it is clicked. We included these IDs so we can find them when we specify the modal box's properties. The style="display:none" will cause the element not to show up on the page (it is good practice to include this definition in the CSS file, but we will specify it in the element for this example).

Now that we have the HTML in place, we can create the javascript to create the modal box. we will be leveraging an IGLOO class: ModalBox, which creates the modal boxes for a number of system elements. To use this for our own box, we will add the following into our javascript file:

window.addEvent('domready', function(){
    //Get the trigger and content for the modal box
    var trigger = $('example-modal-box-trigger');
    var content = $('example-modal-box-content');

    //When the link object is clicked, create the box
    trigger.addEvent('click', function(e){
            //create the modal box
           var modal = new Modalbox({
               'ismodal':true,
                'width':200,
                'height':300,
                'title':'My Modal Box',
                'closetext':'Close',
                'position':{
                    'screen':['middle','center']
               },
               'adopt': $(content)
        });

       //stop the event from going to the link
        e.stop();
    });
});

This might seem a bit complicated at first, so let's break down the code to make sense of it.

window.addEvent('domready', function(){
As explained in the last article, this causes the code inside of it to run when the elements are loaded into the document
    var trigger = $('example-modal-box-trigger');
This will get the trigger element in the HTML document
    var content = $('example-modal-box-content');
This will get the content element in the HTML document
    trigger.addEvent('click', function(e){
Similar to the window.addEvent, but this tells the document to run the following code when the link element has been clicked by a user
        var modal = new Modalbox({
This simply tells the script that we want to create a modal box. the items inside of it (ismodal, width, height) are it's properties.
            'ismodal':true,
This tells the script if it should create a shadow over the page, so that users cannot keep going until they close the box. In this case we do want show the shadow
            'width':200,
this defines the width (in pixels) of the box
            'height':300,
this defines the height (in pixels) of the box
            'title':300,
This defines what text to use for the title of the box.
            'closetext':300,
This defines what text to use for the link that closes the box.
            'position':{
This will define where the modal box is located
                'screen':['middle','center']
this tells the script to create the modal box in the middle of the page
            'adopt': $(content)
This tells the script what element should become the modal box (in this case, the example-modal-box-content element)
        e.stop();
this simply stops the link tag from going to where the link is. If this is left out, the page would go back to the top.

With this script in place, once you click upon "Click Here for a modal box", it will show up. An example of this can be found here.

Although this will work for one modal box, how to we setup multiple modal boxes on a page? We will need to learn how to find classes, and go through each of them to create modal boxes! Next time we will learn about iterating through a number of elements to create multiple modal boxes.

About the author

Andrew Lawton

Andrew Lawton

Web DeveloperIGLOO Inc.

After graduating Conestoga College's computer Programming course, Andrew Lawton braved the harsh world of freelance web application development. After 3 years of explaining that there is no…

0 Comments

Would you like to comment?

You must be a member. Sign In if you are already a member.

  • 558 views
  • $obj.VersionIndex versions
  • 0 comments
  • 1 follower
     
Avg. Rating:
Rating: 4out of5Igloo.Common.ObjectUserInfo
Post Date:
October 5, 2009
Posted By:
Andrew Lawton

Viewed 558 times