views:

220

answers:

3

http://www.ericmmartin.com/projects/simplemodal/

There's two examples near the bottom of the page that show how to either do open or closing animations.

I'm pretty new to jquery. How can I declare both onOpen and onClose? (I've only been able to get one to work at a time.

jQuery(function ($) {
    $('a.basic').click(function (e) {
        e.preventDefault();
        $('#basic-modal-content').modal(
            {
                onClose: function (dialog)
                {
                    dialog.container.fadeOut('slow', function () {
                    });
                }
            }
        );
    });

});

Thanks for any help you can provide.

A: 

You just need to define the onOpen option in your object mapping:

function myOpen(dialog){
    // Do something with the dialog
}
function myClose(dialog){
    // Do something else with the dialog
} 
$('#basic-modal-content').modal({ onOpen: myOpen, onClose: myClose });

Or, you can declare the callbacks in the function call like you did in your post:

$('#basic-modal-content').modal({
    onOpen: function(){
        // Do something with the dialog
    },
    onClose: function(){
        // Do something else
    }
 });
bobthabuilda
Is there a way to set it to work with any modal window? Instead of having to declare it every time for every dif (in this case "basic-modal-conent') is there a way to make the animation be the same for all modal windows?also, for some reason when I put onOpen and onclose the way you suggested, when the window opens it closes right afterwards
Matthew
To target all modals, you should just add a 'modal' class to each DOM element you need it on, then target $('.modal'). Can you post your exact code for onOpen, and onClose?
bobthabuilda
The thing is, each one is unique. For example there may be two links on a page. One link titled "Register" another titled "contact". The link titled register should open the div with id of "simplemodal-register" and the contact should open "simplemodal-contact". I'm going to have several of these on the website, so I'd rather condense the code by not rewriting it for every div I need.The example code is here: http://www.ericmmartin.com/projects/simplemodal/#example-10Thanks for your help.
Matthew
What are the differences between each unique modal? If you aren't changing the css or size of the modal, then the class implementation @bobthabuilda mentions would be the best way. Otherwise you are going to have to style each modal separately, which seems like bad design in my opinion.
ryanulit
A: 

After reading your latest comment, I agree with ryanulit. However, here's one solution to achieve what you've described. You could do something like this for contact and register links:

<a href="#">Contact</a>
<a href="#">Register</a>

You could do something like this:

$('a[href=#]').click(function(){
    // Will reference 'contact' or 'register'
    var modalType = $(this).text().toLowerCase();
    $('simplemodal-' + modalType).modal({ /* Options... */ });
});
bobthabuilda
A: 

Answer to one of the questions:

If you want to set an option once for all modals to use, you can do it with:

$.modal.defaults.onClose = function (dialog) {
     // your code
     $.modal.close();
}
Eric Martin