Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically trigger fancybox with an inline div?

I want to make a fancybox appear when someone tries to submit a form. So I've got this:

$('#login-form').submit(function(e) {
    $.fancybox({
        content: '<h2>Hello Fancybox</h2>',
        modal: true
    });
    return false;
});

Works good, but I'd rather use my div than trying to specify all the HTML in the content string. Can I make it popup with this

<div style="display:none" id="access-policy">
blah blah blah
</div>

Instead?

like image 796
mpen Avatar asked Apr 27 '11 22:04

mpen


People also ask

How do I open fancyBox on button click?

jQuery(document). ready(function($) { $('button'). on('click', function() { $. fancybox(); }); });

What is fancyBox in Javascript?

fancyBox is a tool that offers a nice and elegant way to add zooming functionality for images, html content and multi-media on your webpages. More information and examples: http://www.fancyapps.com/fancybox/ License: http://www.fancyapps.com/fancybox/#license.

How do I add a title to fancyBox?

fancybox({ beforeLoad: function() { this. title = $(this. element). attr('caption'); } });


1 Answers

For FancyBox v3 or later see this answer

For old versions:

You need to set in href property the div ID. If you use $('#access-policy').show() in content property the second time you open this fancybox will show nothing.

$('#login-form').submit(function(e) {
    $.fancybox({
        href: '#access-policy', 
        modal: true
    });
    return false;
});

Also in the HTML has to be:

<div style="display:none">
    <div id="access-policy">
        blah blah blah
    </div>
</div>

:)

like image 126
Marçal Juan Avatar answered Sep 20 '22 09:09

Marçal Juan