Search code examples
javascriptjqueryfancybox

Show a div with Fancybox


On the click of a button, I'm trying to show a simple hidden div in a fancy box. This is the code that works:

$("#btnForm").fancybox({ content: $("#divForm").html() });

But from what I've read, this doesn't seem to be the standard way to accomplish this. I've tried each of the following, unsuccessfully:

$("#btnForm").fancybox({ href: "#divForm" });

$("#btnForm").click(function () {
    $.fancybox({ href: "#divForm" });
});

$("#btnForm").click(function () {
    $("#divForm").fancybox();
});

Can someone point me in the right direction on how to properly use this utility? Here's my html:

    <input type="button" value="Load Form" id="btnForm" />

    <div id="divForm" style="display:none">
        <form action="tbd">
            File: <input type="file" /><br /><br />
            <input type="submit" />
        </form>
    </div>

Solution

  • As far as I know, an input element may not have a href attribute, which is where Fancybox gets its information about the content. The following code uses an a element instead of the input element. Also, this is what I would call the "standard way".

    <html>
    <head>
      <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
      <script type="text/javascript" src="http://fancyapps.com/fancybox/source/jquery.fancybox.pack.js?v=2.0.5"></script>
      <link rel="stylesheet" type="text/css" href="http://fancyapps.com/fancybox/source/jquery.fancybox.css?v=2.0.5" media="screen" />
    </head>
    <body>
    
    <a href="#divForm" id="btnForm">Load Form</a>
    
    <div id="divForm" style="display:none">
      <form action="tbd">
        File: <input type="file" /><br /><br />
        <input type="submit" />
      </form>
    </div>
    
    <script type="text/javascript">
      $(function(){
        $("#btnForm").fancybox();
      });
    </script>
    
    </body>
    </html>
    

    See it in action on JSBin