Colorbox: call colorbox.close

I am working on a project that uses a speed template system, so the $ character is reserved and cannot be used in javascript variable names.

So I have to prefix jQuery variables and methods using jQuery, not $, for example. jQuery (document) .ready (function () {}); unlike $ (document) ready (function () {});

This is normal, but in this case I am using colorbox.

My code for calling colorbox works fine and looks like this:

jQuery(document).ready(function () { jQuery("#addUser").colorbox({ href:"add", width:"500px", onClosed: function (message) { dataTable.refresh(jQuery("#ajaxResult").text(message)); } }) ... }) 

I have a link inside colorbox to which I want to add the colorbox.close method, but when I click the link, I get this error:

Unprepared TypeError: unable to call close method undefined

This is my code:

 jQuery(document).ready(function () { jQuery("a").click(function() { jQuery.colorbox.close("User added succesfully"); }); ... }) 

Can someone tell me why I can not close the colorbox?

By the way, the X that comes with colorbox still works to close it.

+4
source share
4 answers

The easiest way to solve this for me is to store jQuery.colorbox as a variable in the global namespace. Yucky, but it works.

Here is what I put in the parent window:

 jQuery(document).ready(function () { colorbox = jQuery.colorbox; ... }) 

Then so I call it:

 jQuery(document).ready(function () { jQuery("a").click(function() { colorbox.close("User added succesfully"); }); ... }) 
+2
source
 jQuery("#addUser").colorbox.close("User added succesfully"); 

In addition, you should be able to use the $ syntax for jQuery if you select javascript <script type="text/javascript" src="my_script_file.js" /> or escaping $ as \$ using external files .

+6
source

This worked for me:

 jQuery().colorbox.close(); 
+1
source

try writing jQuery.fn.colorbox.close ("User added successfully"); instead of jQuery.colorbox.close ("User added successfully");

0
source

Source: https://habr.com/ru/post/1345258/


All Articles