37

On zurb foundation's website at http://foundation.zurb.com/docs/reveal.php they listed some Options including

open: callback function that triggers 'before' the modal opens.

opened: callback function that triggers 'after' the modal is opened.

close: callback function that triggers 'before' the modal prepares to close.

closed: callback function that triggers 'after' the modal is closed.

But I have no idea how to use them with my modal. I tried:

$('#myModal').closed(function()
{});

$('#myModal').trigger('reveal:closed')(
{});

$('#myModal').reveal.closed(function()
{});

$('#myModal').reveal().closed(function()
{});

I have Googled but found no hits. Anyone who can explain it or give me an example or provide a related link?

The suggestion given works, however I have yet another closely related question for reveal():

<a href="#" class="button" data-reveal-id="myModal2">Click Me For A Modal</a>);

I tried to add one attribute like data-closeOnBackgroundClick="false" That doesn't seem to work. What should be the correct syntax? Will it work for callback function as well?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1882562
  • 373
  • 1
  • 3
  • 6

9 Answers9

41

The above answer did not work for me. Here's what worked (Foundation 4 and jQuery):

$('#myModal').bind('opened', function() {
  console.log("myModal opened");
});
meatrobot
  • 795
  • 1
  • 6
  • 6
  • 2
    Thanks, that worked for me and made this cryptic message from the foundation docs clearer: "Reveal options can only be passed in during initialization at this time. However, you can bind to the open, opened, close, and closed events." – Daniel Ristic Jul 02 '13 at 15:56
21

Event Bindings for Zurb Foundation Reveal -

There are a series of events that you can bind to for triggering callbacks:

$(document).on('open.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('opened.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('close.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

$(document).on('closed.fndtn.reveal', '[data-reveal]', function () {
  // your code goes here...
});

If you have multiple data-reveal used in single page as follows :

<div class="content reveal-modal" id="element-1" data-reveal>
<div class="content reveal-modal" id="element-2" data-reveal>

Then in this situations you can trigger callback same as explained above but with little modification as shown below :

$(document).on('open.fndtn.reveal', '#element-1[data-reveal]', function () {
  // your code goes here...
});

$(document).on('open.fndtn.reveal', '#element-2[data-reveal]', function () {
  // your code goes here...
});
Jabari
  • 5,359
  • 3
  • 26
  • 32
Kedar.Aitawdekar
  • 2,364
  • 1
  • 23
  • 25
  • I'm having trouble returning control to the parent view when the modal is closed. Perhaps you can help me. My question is here: https://stackoverflow.com/q/49161370/1735836 – Patricia Mar 08 '18 at 16:03
  • In Foundation 6, replace: fndtn.reveal with zf.reveal, else this won't work. – Loosie94 Apr 06 '21 at 11:54
16

Call reveal like you normally would, but include the name of the option and corresponding function as an object:

//Reveal the modal and say "Good bye" when it closes
$("#myModal").reveal({ "closed": function () { alert("Good bye") } });
chrisjsherm
  • 1,269
  • 13
  • 17
  • I'm having trouble returning control to the parent view when the modal is closed. Perhaps you can help me. My question is here: stackoverflow.com/q/49161370/1735836 – Patricia Mar 08 '18 at 16:58
15

On Zurb Foundation v6, these events were renamed to xxx.zf.reveal:

  • closeme.zf.reveal Fires immediately before the modal opens. Closes any other modals that are currently open
  • open.zf.reveal Fires when the modal has successfully opened.
  • closed.zf.reveal Fires when the modal is done closing.

Source: http://foundation.zurb.com/sites/docs/reveal.html#js-events

Examples:

  • Generic callback that will fire for all modals:

    $(document).on('open.zf.reveal', '[data-reveal]', function() {
      console.log('This works');
    });
    
  • Callback that will fire when a specific modal is opened:

    $(document).on('open.zf.reveal', '#<ELEMENT-ID>[data-reveal]', function() {
      console.log('This works');
    });
    
Floyd
  • 2,252
  • 19
  • 25
sequielo
  • 1,541
  • 1
  • 18
  • 27
  • 1
    On that note, you can also use this format: $('#myModal ').bind('closed.zf.reveal', function() { console.log("myModal closed!"); }); – JustinJason May 12 '17 at 15:04
  • @JustinJason The difference is that added examples will take care of forms added dynamically, while yours won't. Although that's a jQuery discussion – sequielo May 13 '17 at 16:30
4

Like meatrobot said, to get this working you want to bind to the modal with the action you are targetting. I got this to work:

$('#myModal').bind('closed', function() {
    console.log("myModal closed!");
});
Chris Roane
  • 129
  • 1
  • 3
2

This is a little late but for the second part you add the attributes as a semi-colon separated list of values in the data-options attribute (tested with foundation 5), i.e:

<div id="myModal" data-options="close_on_background_click:false;" class="reveal-modal">

And no, you cannot pass functions this way, i tried :)

James
  • 557
  • 7
  • 16
  • for lightweight passing of options to a particular modal, this is definitely the way to go for foundation 5! not for callbacks like the OP wanted, but config options. – andy Jul 13 '14 at 16:39
  • You save my day. I cannot find that feature in Foundation Docs. Thanks! – Cartucho Feb 04 '15 at 16:24
1

Looking at Foundation 5 and found that the reveal library triggers open, opened, close, and closed events. Just attach a handler to the event you want.

$('#myModal').on([event], handler)

You can also pass the handlers via the settings argument. Check this out: https://github.com/zurb/foundation/blob/master/js/foundation/foundation.reveal.js#L92

L84
  • 45,514
  • 58
  • 177
  • 257
djtek
  • 176
  • 1
  • 3
1

The foundation 5 documentation specifies scoping of reveal events to the 'reveal' eventspace. However, the actual modal events do not seem to fire consistently within this eventspace. Removing this specification fixes the issue:

$(document).on('opened.fndtn', '[data-reveal]', function() {
  console.log('This works');
});
Brian Peacock
  • 711
  • 8
  • 20
0

In foundation 3.2.5 you should bind 'reveal:opened' like this:

$('#myModal').bind('reveal:opened', function() {
   console.log("myModal opened");
});

Regards, MarianoC.

MarianoC
  • 341
  • 3
  • 3