javascriptlightgallery

LightGallery Video Plugin won't work (undefined $)


I am currently working on a webpage and tried implementing LightGallery. Although the image galleries work totally fine for me the videos won't seem to play. When I check the console the following error message is displayed upon loading the site:

<Uncaught TypeError: $.fn.lightGallery is undefined
<anonymous> http://localhost/js/lg-video.js:352
<anonymous> http://localhost/js/lg-video.js:354
<anonymous> http://localhost/js/lg-video.js:17
<anonymous> http://localhost/js/lg-video.js:19

I am fairly new to using LightGallery and to finding out, what the problem in a JavaScript file is since I am not an 'expert' coder.

I used the code snippet from the lg-video plugin website to test it out:

<head>
...
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
...
</head>
<body>
    <script src="js/lightgallery.js"></script>
    <script src="js/lg-video.js"></script>
    <div id="lightgallery">
        <a href="https://www.youtube.com/watch?v=meBbDqAXago" data-poster="video-poster1.jpg" >
            <img src="img/thumb1.jpg" /></a>
    </div>
    <script>
        lightGallery(document.getElementById('lightgallery'));
    </script>
</body>

Solution

  • Here's a working JSFiddle. Here's what I did, working through the installation page I referenced yesterday:

    1. Starting in the "Include CSS and Javascript files" section, they say to include lightgallery.css, though they don't give a link for it. I searched and found a CDN link, and added that ;

    2. Added a CDN link to the latest jQuery;

    3. Copied and added the 'jsdelivr collection' link for the Lightgallery JS;

    4. Copied the JS they show to initiate the plugin;

    5. Copied the relevant part of your HTML snippet - just the lightgallery div, with some minor updates:

      • Neither video-poster1.jpg nor img/thumb1.jpg exist here of course, so I replaced them with placeholder images;
      • I get "Video unavailable" for that video, so I randomly picked another;

    This works fine - the placeholder thumbnail is shown, when I click it the video opens against a black background, and plays if I click play. Here's the code:

    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/1.10.0/css/lightgallery.min.css">
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
            <script src="https://cdn.jsdelivr.net/combine/npm/lightgallery,npm/lg-autoplay,npm/lg-fullscreen,npm/lg-hash,npm/lg-pager,npm/lg-share,npm/lg-thumbnail,npm/lg-video,npm/lg-zoom"></script>
        </head>
    
        <body>
            <div id="lightgallery">
                <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" data-poster="https://via.placeholder.com/150"><img src="https://via.placeholder.com/150" /></a>
            </div>
    
            <script>
                $(document).ready(function() {
                    $("#lightgallery").lightGallery();
                });
            </script>
        </body>
    </html>