Using the click() event to evoke FancyBox

How can I make a FancyBox overlay appear when a DIV is clicked?

I have searched for ways to trigger FancyBox through an Anchor click, but none of the solutions address my specific situation. I need to use a DIV instead of an Anchor because my clickable overlay will contain multiple block elements, and it is not recommended to have an inline element spanning block elements.

Below is a simplified version of the code I am working with (the complete code will be included in a JSfiddle link at the end):

<div id="eAcon6LPgHo" class="youtube_box">
    <img src="http://img.youtube.com/vi/eAcon6LPgHo/0.jpg"/>
    <span class="caption">
        <h3>Dan Bull Raps Fast</h3>
    </span><!--/.caption-->
</div><!--/.youtube_box-->

.

$( document ).ready(function() {
    $( ".youtube_box" ).click(function() {
        var YouTubeID = this.id;
        // alert( "clicked: " + YouTubeID );
        $.fancybox({
            href : 'http://www.youtube.com/embed/' + YouTubeID + '?autoplay=1'
        });
    });
});

When the DIV is clicked, the YouTube ID is retrieved from the element ID (I confirmed this with the commented out alert). However, FancyBox does not get triggered, and there are no error messages shown.

You can view the full code on JSfiddle here: http://jsfiddle.net/5pd6egbo/

I would greatly appreciate your assistance!

Answer №1

To make it simple, you can utilize the unique data-fancybox attributes within your <div> tag like this:

<div id="jRgk28LdmNQ" class="video_box" data-fancybox-href="http://www.youtube.com/embed/jRgk28LdmNQ?autoplay=1" data-fancybox-type="iframe">
    <img src="http://img.youtube.com/vi/jRgk28LdmNQ/0.jpg"/>
    <span class="description">
        <h3>Amazing Acrobatics Compilation</h3>
    </span><!--/.description-->
</div><!--/.video_box-->

After that, include the standard fancybox initialization script:

jQuery(document).ready(function ($) {
    $('.video_box').fancybox();
}); // ready

No need to rely on the .click() method anymore.

Check out the example on JSFIDDLE

Answer №2

By specifying the type as iframe, the functionality seems to be fixed.

$( document ).ready(function() {
   $( ".youtube_box" ).click(function() {
      var YouTubeID = this.id;

      $.fancybox({
          href : 'https://www.youtube.com/embed/' + YouTubeID + '?autoplay=1'
      }, {
          type: 'iframe'
      });
   });
}); 

To see a working example, check out this Fiddle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to add multiple classes to an HTML element?

Can a single HTML container have more than one class assigned to it? For example, would the following code work: <article class="column wrapper"> ...

Preserving distance between absolute elements

I'm facing an issue with my menu animation using jQuery. In order to achieve the desired effect, I am forced to use position: absolute. My challenge is maintaining consistent spacing between each text string, especially since they vary in width. Unfor ...

Is it feasible to send information to a database without the utilization of PHP?

I need to find a way to submit form data to a database without using PHP since the third-party platform I'm working on doesn't support it. Is there an alternative method to accomplish this task? After researching, I haven't found a clear so ...

Issues with Nav pills (bootstrap 4) not properly setting active states

I am attempting to add a background color to the active state of the Nav Pill. Here is the HTML and CSS code I am using: .nav-pills > .nav-item > .nav-link:active{ background: red!important; color: white!important; } .nav-pills > .nav-ite ...

None of the angular directives are functioning properly in this code. The function attached to the submit button is not executing as expected

I've experimented with various Angular directives in this code, but none seem to be functioning properly. I'm wondering if a library file is missing or if there's some issue within the code itself, potentially related to the jQuery file. The ...

displaying and concealing elements with jquery

Is there a way to hide a div if the screen size exceeds 700px, and only show it when the screen size is less than 700px? Below is the jQuery code I'm attempting to use: jQuery(document).ready(function() { if ((screen.width>701)) { $(" ...

Determine if I'm actively typing in the input field and alter the loader icon accordingly with AngularJS

Just delving into the world of AngularJS and attempting to tweak an icon as I type in an input box. Currently, I can detect when the box is focused using the following code: $scope.loading = false; $scope.focused = function() { console.log("got ...

CSS guidelines for layering shapes and divs

Currently, I am in the process of developing a screenshot application to enhance my understanding of HTML, CSS, and Electron. One of the key features I have implemented is a toggleable overlay consisting of a 0.25 opacity transparent box that covers the en ...

Having trouble getting the jQuery validate function to work properly with a select element

I have successfully implemented a validate function for my input fields : $.tools.validator.fn("#password", function(input, value) { return value!='Password' ? true : { en: "Please complete this mandatory field" }; }); $("# ...

Customizing GTK with CSS: Altering button background color on click event

Currently I am working on creating an application using GTK and styling it with CSS. One of the requirements is to change the background of a button when clicked, specifically for a built-in video player. The desired behavior is to have the 'play&apos ...

Ensure Password is Secure (Django/Python)

I'm currently working on a small Django app for educational purposes, utilizing the built-in User model provided by Django. My focus is on learning its features and functionality. In order to interact with users, I've created custom pages that al ...

Manipulate the text within a canvas element using JavaScript

In this scenario, suppose json.mes represents the received message from Ajax. There is a button implemented to display the message in a canvas box. Although I have attempted to retrieve the message using getElementById (as shown below), it seems to be in ...

What is causing this particular jQuery Ajax request to fail exclusively in IE9, when it functions perfectly in both IE8 and IE7?

When making an ajax call on my website, I encountered a strange issue with IE9. The script works perfectly fine on every browser except for IE9. Despite the fact that the page being requested returns a single digit status code and is hosted on the same dom ...

Is it possible to utilize Bootstrap Icons as bullet points on a website?

Is it possible to use Bootstrap icons for bullet points with CSS? I am aware that FontAwesome can be used for this purpose, but I would like to confirm if the same can be achieved using Bootstrap. Though there is a method using: <ul> <li>< ...

Modify a single row within a column with Jquery

I have been working on a project that involves using jQuery with JSON data to populate an HTML table. However, I am facing an issue where when I try to do some inserts, I need to update multiple rows in one column, but it is not functioning as expected - i ...

Show JSON information in a JSP page and an HTML file simultaneously

As a Java newbie, I am facing an issue with my test.json file which is in an array format like this: { "Result": "OK", "TotalRecordCount": 23, "Records": [{ "vEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data- ...

Unable to adjust the width of a datatable using the jQuery Datatable plugin version 10.1

I am facing a challenge with multiple datatables using the jQuery plugin on my page. Some of the datatables fit perfectly within the page width, while others extend beyond the page boundaries. Despite trying various methods, including setting styles in HTM ...

Issues with jqplot functionality when placed in an Accordian nested inside a Tab

My current project involves using jqplot to display graphs within an accordion. If you're interested, you can check out jqplot here: Initially, everything worked smoothly when displaying the graphs without the accordion feature. However, when I attem ...

Using md-chips and md-select together in multi select mode

I'm having trouble generating md-chips when selecting multiple values from an md-select. Does Md-chips only work with autocomplete analyzers and input fields? <md-chips ng-model="launchAPIQueryParams.type"> <md-select name="launchCalTy ...

Having trouble resolving a setInterval problem with JavaScript?

Yesterday, I learned about the setInterval function which allows me to execute a task or function after a specific amount of time. While I have successfully implemented the interval in my code, it keeps adding new lines with the date each time. What I re ...