Search code examples
javascripthtmljquery-pluginsprettyphoto

Custom function not showing JQuery plugin


I'm a newbie in Jquery. I'm trying to print text on a document dynamically. I have a JavaScript function that is supposed to display images using the jQuery prettyphoto plugin. My function does not show the images but when I paste the code on a html file it works fine. Please help. A sample of my code is here.

    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" href="./index.css" type="text/css">
    <script type="text/javascript" charset="utf-8" src="jquery-1.6.4.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="spin.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="custom-functions.js"></script>
    <link rel="stylesheet" href="./prettyPhoto/css/prettyPhoto.css" type="text/css">
    <script type="text/javascript" src="./prettyPhoto/js/jquery.prettyPhoto.js"></script>

    <script type="text/javascript">
    $(document).ready(function()
    {
    $("a[rel^='prettyPhoto_related-content-images']").prettyPhoto({theme:'facebook'});
    });

 function showImages()
    {
    var stringData = '';

    stringData = stringData + '<div id="related-content-images" >Related to this: <br/>'+
    '<a href="images/sample-album-2.jpg" target="" rel="prettyPhoto_related-content-images[related-content-images]" title=""><img id="not-hidden" src="images/sample-album-2.jpg" alt="" title=""></a>'+
    '<a href="images/sample-album-3.jpg" target="" rel="prettyPhoto_related-content-images[related-content-images]" title=""><img id="related-images" src="images/sample-album-3.jpg" alt="" title=""></a>'+
    '<a href="images/sample-album-4.jpg" target="" rel="prettyPhoto_related-content-images[related-content-images]" title=""><img id="related-images" src="images/sample-album-4.jpg" alt="" title=""></a></div>';
    document.getElementById("content").innerHTML=''+stringData+'';
    }
    </script>

    </head>
    <body>
    <div id="header"><div id="app-logo-image">
    <img src="images/app-logo.png" id="app-logo"/></div>
    <div id="header-form">
    <input type="search" id="search-text-box" name="search-text-box" placeholder="Search">
    <input type="submit" id="search-button" name="" value="Search">
    </div>
    </div>
    <div id="container">
    <div id="sub-header-menu">
    <div id="music-albums">
    <img src="images/music-albums-icon.png" id="music-albums-image"; onclick="fetch('music-albums');"/></div>
    <div id="genres-icon">
    <img src="images/genres-icon.png" id="genres-icon-image"onclick="fetch('music-genres');"/></div>
    <div id="artist-icon">
    <img src="images/artists-icon.png" id="artist-icon-image"onclick="fetch('music-artists');"/></div>
    <div id="home-icon">
    <img src="images/home-button.png" id="home-icon-image"/></div>
    </div>
    <div id="content" style="left:0%;">
    <a href="#" onclick="showImages();">click me</a>
    </div>
    </div>
    </body>
    </html>

Solution

  • Your showImages function isn't executed until the link is clicked, however the prettyPhoto initialisation is only run once when the document finishes loading, and since showImages hasn't been run yet prettyPhotos can't find and enhance those images.

    If you add this to the end of showImages everything should work:

    $('#content a').prettyPhoto({theme:'facebook'});