Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function after FancyBox loaded

I am trying to get FancyBox to load an file which contains a form. I want to use the jQuery Validate plugin to validate, but the form is not being added to the DOM before the callback is made.

The afterLoad option does open an alert, but it actually opens before the ajax content is loaded.

I can add an href in the loaded file that when clicked runs validate(), but interestingly, it only works if I call the form by ID rather than class, as there is another form on the same page with that class.

I have the following code:

<a href="/url" class="fancy fancybox.ajax">link</a></li> 



$(".fancy").fancybox({
    maxWidth    : 800,
    maxHeight   : 600,
    fitToView   : false,
    width       : '70%',
    height      : '70%',
    autoSize    : false,
    closeClick  : false,
    openEffect  : 'none',
    closeEffect : 'none',
    afterLoad: function() {
      //$(".validateMe").validate();
      alert(987987);
    }
});


<form id="someId" action="javascript:someFunction();" class="validateMe" method="post">
like image 773
Jazzy Avatar asked Dec 11 '12 18:12

Jazzy


People also ask

How do I open fancyBox on button click?

on('click', function() { $. fancybox(); }); });

What is fancyBox in Javascript?

Fancybox saves you time and helps to easily create beautiful, modern overlay windows containing images, iframes, videos or any kind of HTML content. This is the 4th generation of Fancybox and brings lots of fresh features.

What is fancyBox in HTML?

fancybox is designed to display images, video, iframes and any HTML content. For your convenience, there is a built in support for inline content and ajax. Images.


2 Answers

Try using the ajax option with its complete event:

$(".fancy").fancybox({
    // ...
    ajax: {
        complete: function(jqXHR, textStatus) {
            $(".validateMe").validate();
        }
    }
});
like image 130
slackwing Avatar answered Sep 29 '22 08:09

slackwing


I was in a similar situation where I wanted to do some DOM manipulation after the content loaded, but afterLoad did not work. I had to use afterShow instead, which seems to be very similar to onComplete from Fancybox v1.3.

$.fancybox({
            href: '/some/link/here',
            type: 'ajax',
            // Other options like size go here
            afterShow: function() {
                // Code to execute after the pop up shows
            }
        });
like image 32
The Unknown Dev Avatar answered Sep 29 '22 08:09

The Unknown Dev