javascriptfancybox

how to open fancybox from javascript.


I have the following code in my cshtml file.

<div style="display:none">
<div id="EditFancy" class="fancybox-infobar">
//Some not relevant input fields. 
</div>
</div>

I'm trying to activate/show that fancybox from a javascript which is included at the top of the cshtml file.

However I'm able to activate/show it from a link within the same cshtml file. With the following code:

    <a href="#EditFancy" class="btnForm" id="btnForm">asd</a>
     <script type="text/javascript">
        $("#btnForm").fancybox();
    </script>

Help will be greatly appreciated.


Solution

  • You could create your own click handler using event-delegation that opens fancybox, for example:

    $( "body" ).on( "click", "btnForm", function() {
      $.fancybox.open({ src: $(this).attr('href'), type : 'inline' });
    });