Is it possible to have separate settings for two different Colorbox popups on the same page?

I stumbled upon a solution for handling multiple colorboxes on the same webpage over at this Stack Overflow post

function applyColorboxTheme() {
    var selectedTheme = $(this).attr("data-theme");

    $(".colorboxTheme").attr("disabled", "disabled");
    $("#" + selectedTheme).removeAttr("disabled");
}

function resetColorboxTheme() {
    $(".colorboxTheme").attr("disabled", "disabled");
    $(".colorboxTheme.default").removeAttr("disabled");
}

$(document).ready(function(){
    $("a.colorbox").colorbox({
        onOpen: applyColorboxTheme,
        onClosed: resetColorboxTheme,
        iframe:true,
        innerWidth:940,
        innerHeight:375,
        loop: true,
        slideshow: false,
        slideshowAuto: true,
        slideshowSpeed: 2500,
        slideshowStart: "start slideshow",
        slideshowStop: "stop slideshow",
    });
});

While this code snippet works well, it only allows for styling the colorboxes differently using separate CSS files. However, all colorboxes are still bound to the same behavioral options in the JavaScript. Is there a way to have distinct CSS and JS settings for each colorbox instance?

Answer №1

After some experimentation, I managed to come up with a solution for this issue. While it may not be the most straightforward method, it does get the job done effectively.

Here is the code snippet in the HTML document:

<head>        
    <link id="colorboxHtml" rel="stylesheet" type="text/css" href="styles/colorboxHtml.css">
    <link id="colorboxYoutube" rel="stylesheet" type="text/css" href="styles/colorboxYoutube.css">
</head>
<body>
        <section>
            <a class="html" href="includes/colorboxHtml.html">Web page in colorbox
            </a>                    
        </section>
        <section>
            <a class="youtube" href="http://www.youtube.com/embed/8C0NfQrky6I">Youtube in colorbox
            </a>
        </section>
    <!--footer-->
    <script src='scripts/colorboxHtml.js'></script>
    <script src='scripts/colorboxYoutube.js'></script>
    <script>
        $(document).ready(function(){
            jQuery(".html").colorboxHtml({iframe:true, innerWidth:"80%", innerHeight:500});
            jQuery(".youtube").colorboxYoutube({iframe:true, innerWidth:640, innerHeight:390});
        });
    </script>
</body>

Now, we need to create custom JavaScript and CSS files:

  1. Open the colorbox.css file and replace every occurrence of "colorbox" with "colorboxHtml".
  2. In the same file, replace every instance of "cbox" with "cboxh".
  3. Save this edited file as colorboxHtml.css.
  4. Repeat the find-and-replace process in the colorbox.js file, saving the modified version as colorboxHtml.js.
  5. You can customize other instances of colorbox by repeating these steps.

By creating separate CSS and JS files for each instance, we have the flexibility to style and control the functionality of each element individually.

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

Can a fixed div be made to adapt to different screen sizes?

Struggling to make SVG data charts responsive due to the 'position:fixed' CSS attribute applied, I'm exploring alternative solutions that don't involve media queries. Ideally, I want the SVG to scale up and down while remaining centered ...

Experience the simplicity of running basic Javascript Scratch code within IntelliJ IDEA Ultimate

Is there a straightforward way to run and debug a basic JavaScript code in IntelliJ Idea Ultimate without the need for additional setup like creating an HTML file or npm project? I'm looking to avoid boilerplate tasks and wondering if there's an ...

Is it possible to choose only half of the elements using the :nth-child selector in

Consider this code snippet: <ul> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> </ul> Can we select exactly half of the total elements by ...

Customize the position values of the Ngx-bootstrap Tooltip manually

I have incorporated ngx-bootstrap into my Angular 4 application. The component I am using is ngx-bootstrap Tooltip: After importing it, I am implementing it in my component's view like this: <button type="button" class="btn btn-primary" ...

Tips for implementing Animation.css in Angular version 1.5.8

I have successfully added the ngAnimate dependency to my AngularJS project: var app=angular.module('testApp',['ngRoute', 'ngAnimate']); I have also included the animation classes in my animation.css file: .slide-animation.n ...

Ways to prevent an element from displaying a hover border or outline

I need help with styling a group of 50% width elements that are displayed next to each other. I want to add a 20px white border around each element to create a separation between them. This is necessary because I have a responsive layout and always require ...

The issue of continuous requests in AngularJS controllers

In my controller, I have a simple function that calculates the number of answers for each question: $scope.countAnswers = function(questionid) { AnswersQueries.getAnswers(questionid, function(answers) { var answersCount = answers.length; return ...

Why is it that GetElements does not provide immediate results upon execution?

Just diving into the world of Javascript for the first time and experimenting with it on Chrome, but running into unexpected results. When I try: document.getElementsByTagName("h1") I anticipate seeing: <h1>tester h1 in body</h1> Instead, wh ...

Angular, display the chosen option within the text input field

Currently, I have a table with multiple columns and I am using Angular JS to display their values in various controls. For instance, I am using a Select/Option (multiple) to display column A, and I want to use another text box to display column B based on ...

What could be causing my Next.js application to not function properly on Safari?

With my current project of developing a web app using nextjs, I'm encountering an issue specifically on Safari browser for Mac. Surprisingly, everything works perfectly fine on other browsers and even on iPhone. Upon opening the developer console, thi ...

How to vertically center the contents of two adjacent divs with automatic width?

I've experimented with a range of HTML elements and CSS styles in an effort to make this work, but without success. How can I achieve the following task? My goal is to have two side-by-side DIVs that automatically adjust their size based on the conte ...

Ways to insert HTML text into an external webpage's div element without using an iframe

Is it possible to generate HTML and SVG code based on the position of a Subscriber on a web page or within a specific div? I would like to provide some JavaScript code that can be inserted inside a particular div on the page. Using an iframe is not ideal ...

The hierarchical structure in the DOM that mirrors an HTML table

While working on code to navigate the DOM for a table, I encountered an unexpected surprise. The DOM (specifically in FF) did not align with my initial expectations. Upon investigation, it appears that FF has automatically inserted a tbody along with sever ...

Add up the values in the table by organizing them into groups

There is a table below that I am working with: <table> <tr> <th>Category</th> <th>Value</th> </tr> <tr> <td class="cat1">cat1</td> <td class="value" ...

"Although the ajax request was successful, the post data did not transfer to the other

i am working with a simple piece of code: var addUser = "simply"; $.ajax({ type: 'POST', url: 'userControl.php', data: {addUser: addUser}, success: function(response){ alert("success"); } }); on the page use ...

Ways to extract information from a dynamically generated table

I'm facing an issue with retrieving data from a dynamically generated table within my script... Here is the code snippet: $('#update_panel').html('Loading Date....'); $.ajax({ url: '/Home/GetCountries', type: & ...

The AJAX request is not being initiated

Upon completion of the animation, I initiate an ajax call as demonstrated below. However, for some reason, the ajax call does not seem to be executing at all. The php file responsible for processing the ajax request is not receiving any data. I am puzzle ...

Gather a linear array of deeply nested information

Below is a dictionary: var objectSchemasList = { 1: [ { name: 'list_field1_1', uuid: 'uuid1', fieldObjectSchemaId: 2 }, { name: 'list_field1_2', uuid: 'uuid2', f ...

Creating arrays of form input elements using Mootools

I searched extensively for a solution to this query, but unfortunately, I could not find the exact answer I was seeking. I am wondering how to access a form input element in mootools that has an array name. I want to use the "double dollar" ($$) function t ...

Bizarre Actions of a JavaScript Object

Currently, I am in the process of developing a Multiplayer game using Phaser and Eureca io. My main focus right now is on perfecting the authentication of players and their controls. To achieve this, I have implemented a method on the server that retrieves ...