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).
I am a Modal Box!
And you can make one too!
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.
4 Comments
Where can I find the documentation for the modalbox? I would like to control exactly where I place the box - how would I do that?
I would like to position my box somewhere else than 'middle' and 'center'. Where can I find the entire breakdown of modalbox properties and options?
Was there ever a follow up for this?
This is awesome. Super helpful.
Are there additional basics blog posts to view?