Search code examples
jqueryfancybox-2

Dynamic content in fancybox 2


I am facing issue getting html of the sibling of a clicked element into the content and showing in a fancybox.

My html is

<a href="#" class="no-republish">Republish</a>
<p class="no-republish-message">Please try again after few days.</p>

My js is

$(".no-republish").click(function(e) {
    $.fancybox({
        type: 'inline',
        beforeShow: function () {
            this.href = $(this.element).next('.no-republish-message').html();
        },
    });
    e.preventDefault();
});

Solution

  • Changing js to the following works perfectly

    $(".no-republish").click(function(e) {
        var c = $(this).next('.no-republish-message').html();
        $.fancybox({
            content: c,
        });
        e.preventDefault();
    });