Haradzieniec
Haradzieniec

Reputation: 9338

display jquery fancybox as ajax success

I have:

The code is below.

<script>
    $(document).ready(function ()
    { //this fancybox appears when <a href="..." ...>...</a> clicked
        $(".fancybox-effects-d").fancybox({
            padding:15,
            closeBtn:true,
        });

        $("form#submit").submit(function ()
        {
            var name = $('#name').attr('value');
            var password = $('#password').attr('value');
            $.ajax({
                type:"POST",
                url:"index/success",
                data:{ name:name, password:password},
                success:function ()
                {
                    //this form disappears and div appears when submit button of the <form id="submit" ...>...</form> clicked
                    $('form#submit').hide(function ()
                    {
                        $('div#errors').fadeIn(3000);

                    });
                }
            });
            return false;
        });
    });
</script>

I want to move that fancybox to appear on ajax success. How should I do that?

Upvotes: 2

Views: 8118

Answers (2)

user1537927
user1537927

Reputation: 66

You can call a fancybox manually with this simple function

$.fancybox(
        '<p>Content of the box in HTML</p>',
        {
                padding:15,
                closeBtn:true
        }
    );

Just add it to the success function.

Upvotes: 3

Rob Angelier
Rob Angelier

Reputation: 2333

You could use a link that is invisible to the end-user and trigger it with jQuery on the ajax success callback. Something like this:

<a id="hiddenlink" href="#fancy" style="display: none;"></a>

<script>

    $(document).ready(function ()
    { //this fancybox appears when <a href="..." ...>...</a> clicked
        $(".fancybox-effects-d").fancybox({
            padding:15,
            closeBtn:true,
            }
        });

        $("form#submit").submit(function ()
        {
            var name = $('#name').attr('value');
            var password = $('#password').attr('value');
            $.ajax({
                type:"POST",
                url:"index/success",
                data:{ name:name, password:password},
                success:function ()
                {
                    $("a#hiddenlink").trigger("click");
                    //this form disappears and div appears when submit button of the <form id="submit" ...>...</form> clicked
                    $('form#submit').hide(function ()
                    {
                        $('div#errors').fadeIn(3000);

                    });
                }
            });
            return false;
        });
    });
</script>

Upvotes: 4

Related Questions