The Colorbox class is not compatible with AJAX or DOM objects

Typically, I utilize the colorbox tool to open "popup windows" on my page and everything works smoothly. However, in my latest project, things are a bit different as I am using js/ajax/dom to dynamically create my objects in the handleRequestStateChange() function.

After importing js, jquery, and css for colorbox, at the head of my js page, I include:

 $(document).ready(function () {
        $(window).scroll(function () {
        //oP1 = document.createTextNode(posizione_menu.offsetTop);
    //divIpt.appendChild(oMtx1);
    $(".divHcss").css("position", "fixed").css("top", "0px").css("z-index", "999");
        });
        //Examples of how to assign the Colorbox event to elements
            $(".group1").colorbox({rel:'group1'});
            $(".group2").colorbox({rel:'group2', transition:"fade"});
            $(".group3").colorbox({rel:'group3', transition:"none", width:"75%", height:"75%"});
            $(".group4").colorbox({rel:'group4', slideshow:true});
            $(".ajax").colorbox();
            $(".youtube").colorbox({iframe:true, innerWidth:640, innerHeight:390});
            $(".vimeo").colorbox({iframe:true, innerWidth:500, innerHeight:409});
            $(".iframe").colorbox({iframe:true, width:"80%", height:"80%"});
            $(".inline").colorbox({inline:true, width:"50%"});
            $(".callbacks").colorbox({
                onOpen:function(){ alert('onOpen: colorbox is about to open'); },
                onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
                onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
                onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
                onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
            });

            $('.non-retina').colorbox({rel:'group5', transition:'none'})
            $('.retina').colorbox({rel:'group5', transition:'none', retinaImage:true, retinaUrl:true});

            //Example of preserving a JavaScript event for inline calls.
            $("#click").click(function(){ 
                $('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
                return false;
            });
    });

Then, in the handleRequestStateChange(), I create an 'a' element and assign it to a div:

var a = createElement('a');
    //a.style.display = "block";
    a.setAttribute('class','iframe');
    a.setAttribute('href',"php/whois.php?P1="+oStxt.value);
    var divIp3 = createElement('div', 'divIp3', 'divIp3css');
    var divIp31 = createElement('div', 'divIp31', 'divIp31css');
    divIp3.appendChild(divIp31);
    divIp3.appendChild(a);
    a.appendChild(divIp31);

The divIp31 becomes linkable, but when clicked, the href opens the page in a normal browser tab instead of utilizing the attribute class for colorbox. Any ideas on how to resolve this?

Thank you in advance.

AM

Answer №1

Make sure to include this line of code:

$(a).colorbox({inline:true, width:"50%"});

once you have appended the anchor element to the div. The previous colorbox call only applied the behavior to elements that were already on the page. Remember to do this for every new element added to the page.

UPDATE: It's important to note that the variable a is referencing a DOM node. Wrap it in jQuery's $ function so that colorbox methods can be used.

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 call the app.js function from an HTML page in an Express.js environment

Is there a way to trigger the function init() { // } located in app.js from an HTML page? <a href='javascript:init();'> Trigger init </a> The issue with the code above is that it searches for function init() only on the client side ...

Is there a way for me to make a local HTML page save text directly to a file on my computer?

Currently, I am working on a straightforward HTML+JavaScript application that is designed to be loaded from a local file and run within the user's browser. The main functionality of this app will be to enable users to input text into a textarea and sa ...

Tips for sending a parameter to an onClick handler function in a component generated using array.map()

I've been developing a web application that allows users to store collections. There is a dashboard page where all the user's collections are displayed in a table format, with each row representing a collection and columns showing the collection ...

Steps for transferring radiobutton value via ajax between two JSP pages:

(document).ready(function() { $("input[type='radio']").on("click", function() { var selectedRadioVal = $("input[name='price']:checked").val(); }); } I want to transfer the value of selectedRadioVal to a different jsp fi ...

Error: Property 'barcodeScanner' is not readable

Currently, I am utilizing barcodescanner.js for scanning QR codes. I have successfully downloaded and linked the CaptureActivity android library to my project. However, when attempting to execute the following code: window.plugins.barcodeScanner.scan(sca ...

When certain triggers are activated, a hidden textbox revealed through javascript is made visible

After changing a dropdown value (from ddlSource) and hiding some text boxes using JavaScript, everything works fine. However, when the user enters a certain value in another textbox triggering an AJAX call to populate some labels, upon form reload, the hid ...

How can we trigger an AJAX function for every checkbox depending on its current state?

My jquery function has a peculiar behavior. Whenever I interact with the checkboxes, whether checking both, unchecking both, or checking one and unchecking the other, only the last checkbox edited triggers the method. In other words, it only 'removes& ...

Unable to view cross domain cookies within the browser's development tools

I am currently working on a web application that has an Angular front end running on http://localhost:4200 and a NodeJs backend running on http://localhost:3000. When a user successfully logs in, a cookie is set from the backend containing a token. However ...

What is the most efficient method to extract information from a jQuery script in web.py?

Seeking a tutorial for jQuery + web.py has been fruitless. As a result, I have a straightforward question regarding the POST method. This is my jQuery script: <script> jQuery('#continue').click(function() { var command = jQu ...

Problem with clicking on items in Slick Slider Variable width menu once they reach the end

Is there a way to stop the slick slider with variable width items from scrolling after reaching the last item? I am encountering this issue where it continues to scroll even after the end. If you want to see the problem in action, check out this fiddle: h ...

accessing various tiers of data through an application programming interface

Currently, I have successfully accessed 5 followers of a GitHub user via AJAX. I am now attempting to delve three levels deep into each of the initial 5 followers, retrieving 5 more followers at each level. Essentially, the goal is to return 5 followers, t ...

data-cy appears in the DOM structure, yet remains unseen

I'm seeking some clarity on how to effectively use Cypress. While writing end-to-end tests, I encountered a failed test due to the element not being visible. A helpful answer I found assisted me in resolving this issue by clicking an element that was ...

Retrieving the selected value from a select2 dropdown in PHP when paired with an input type text

I have implemented multiple select2 dropdowns on my webpage to facilitate sending emails to users. One dropdown is for selecting recipients in the To field, and the other is for selecting recipients in the CC field. I want to ensure that when a user is sel ...

Bot is being inundated with messages containing no content

My discord.js version is 14.0.3. I'm facing an issue where the message content is not being retrieved correctly. To solve this, I set up a client.on('messageCreate') event handler: client.on('messageCreate', async (message) => ...

How to achieve horizontal auto-scrolling in an image gallery with jQuery?

Hey there, I'm currently working on an Image Gallery project. I have arranged thumbnails horizontally in a div below the main images. Take a look at this snapshot img. My goal is to have the thumbnails scroll along with the main pictures as the user ...

Is there a way to organize a specific column based on user selection from a drop down menu on the client side?

When selecting Timestamp from the drop-down menu, I expect the TimeStamp to be sorted in either ascending or descending order. Similarly, if I choose Host, the Host should be sorted accordingly. Despite using the Tablesorter plugin, sorting doesn't s ...

Issue encountered in Django with Jquery: Uncaught TypeError - $(...).draggable is not a function

I am encountering an issue with my Django application that involves Jquery functions not working properly on the frontend. Strangely, these functions work perfectly fine when the HTML/CSS is tested independently outside of the Django environment. In order ...

Highcharts JS encountered an error: x[(intermediate value)(intermediate value)(intermediate value)] is not a valid constructor

I'm in the process of creating a bar chart by fetching options from an ajax response. However, I encountered an error when passing the object to the highcharts constructor. Uncaught TypeError: x[(intermediate value)(intermediate value)(intermediate v ...

When the value is removed, it does not revert back to the initial filtered choices

After clearing the input, I want to display all the original li's again. The issue is that even though .value = '' clears the input, the filter remains active. I could really use some help with this as it's starting to get on my nerves ...

Is it unorthodox to utilize constructor instances as prototypes in "WebGL - Up and Running"?

In the book "WebGL - Up and Running," a unique custom geometry is created in a rather straightforward manner. However, what caught my attention was the penultimate line of code. Here's how it looked: Saturn.Rings = function ( innerRadius, outerRadius ...