jQuery Thickbox not working in IE6 and above MythTV: How to build your own PVR
May 27

The jQuery thickbox plugin is fantastic. All you have to do is include the js file, and give the elements that require a jQuery thickbox, a class of thickbox. Like below;

<a class="thickbox" href="thickboxexample.php?height=600&width=600">jQuery Thickbox Example</a>

The thickbox plugin then automically!! attaches its events to those elements. For ease of use this is great, but it has its limitations, because the events are attached on page load.

What happens when you want to attach a thickbox to an element that is generated via an ajax call?

Well you’re gonna kick yourself! You simply need to re-call the the thickbox initialise function within your ajax call, just like the example below:

$.ajax({
    type:       "GET",
    url:        "/ajax-content.php",
    cache:      false,
    data:       "f=foo&b=bar",
    loading:    $('.loading').html("“),
    success:    function(html) {
                    $(”#ajaxContent”).html(html);
                    //re-attach thickbox
                    tb_init(’a.thickbox, area.thickbox, input.thickbox’);
                }
});

I am assuming that you already know how to make an ajax call using jQuery, so I won’t explain the above code, except for the fact that a call is made to tb_init() after the ajax response. This is the thickbox initialise function which attaches its events to your selected elements.

Easy eh? Forgive me if I’m teaching you to suck eggs!! but it took a while to click for me!

written by admin \\ tags: , ,


53 Responses to “jQuery Thickbox on Ajax Generated Pages”

  1. Martijn Says:

    Tnx mate, you made my day. I was just about to give up when I stumbled on you’re site. I just couldn’t get Thickbox or Lightbox to work after the content was loaded with Ajax. I tried with tb_init() but probably the wrong way.

  2. dave Says:

    My pleasure mate. I like to share these sorts of things. It makes our lives easier. One thing to note on this though, which I stumbled upon recently after writing this article. If there is already content on the page that has a thickbox event attached it will create to events for that click. For example, I had forms loading into a thickbox, from links outside the ajax generated page, so when I called tb_init() again, the forms were loading in the tickbox twice. The way round this was to create a separate function which detached all thickbox events before reattaching them.

    function removeThickBoxEvents() {
            $('.thickbox').each(function(i) {
                $(this).unbind('click');
            });
        }
    
    function bindThickBoxEvents() {
            removeThickBoxEvents();
            tb_init('a.thickbox, area.thickbox, input.thickbox');
        }
    

    Then in your ajax call just call bindThickBoxEvents

    $.ajax({
        type:       "GET",
        url:        "/ajax-content.php",
        cache:      false,
        data:       "f=foo&b=bar",
        loading:    $('.loading').html("“),
        success:    function(html) {
                        $(”#ajaxContent”).html(html);
                        //re-attach thickbox
                       bindThickBoxEvents()
                    }
    });
    

    Thanks for your comments. I’m glad someone is reading my blog!!

  3. Murat Ayfer Says:

    ah, thanks a lot. this really saved me quite a lot of time.

  4. Dave Says:

    Your welcome. I’m glad I could help. Thanks for visiting my site.

  5. Eturama Says:

    The important function that should be called is in the AJAX CALL, or more precise on the success of the AJAX CALL :

    tb_init(’a.thickbox, area.thickbox, input.thickbox’);

    Just to help people that dont know jquery ;à)

    With the YUI it would be here :

    var handleSuccess = function(o) {
    div.innerHTML = o.responseText;
    tb_init(’a.thickbox, area.thickbox, input.thickbox’); // reload thickbox !!!!!!!
    };

    Thank you very much man your post helped me to find the solution.

  6. MaverickKK Says:

    Yahooooo

  7. Quinton Figueroa Says:

    I’m happy I found this post. I thought I was going crazy on why thickbox stopped working as soon as I loaded content in via AJAX.

    Thanks!

  8. Oach Says:

    Wow, this is a huge help. After four days of pure hell and trying everything that I could possibly think up, I have a solution and I can start drinking again for fun! :D

  9. Stephen Says:

    oooops!
    Thank u so much…

  10. nahtass Says:

    Yeah, I just wanted to post a thank you as well. You should see this stuff in action on a site I am developing. Very good. Works like a dream!

  11. Dave Says:

    No problem nahtass. Post your link when your site goes live!

    and thanks to everyone else to who has commented on this post. Everytime I come across a problem I always search the internet for howto’s and guidance. This my way of giving back!!

  12. Juanma Says:

    Thanks, i have problems with my dinamic ajax fields and your last post help me alot. Do you know any visual effects?

  13. Dave Says:

    Glad I could help Juanma!! What type of visual effects are you looking for?

  14. Mike Says:

    I would like to see a bunny pulled out of a hat please.

  15. Roberto Says:

    Great!
    This post solved my problem and your solutions was easy to apply.
    Good job!!

  16. Gokce Yalcin Says:

    Globally removing-adding events on collections will slow down your code. You can bind via tb_init(this) on ajax oncomplete event.

  17. Flaab Says:

    Thanks a lot man, I was going nuts xD

  18. pratik Says:

    tb_init(’a.thickbox, area.thickbox, input.thickbox’); i added this in my ajax (js) file still the thickbox is not showing up its opening normally in another page i would be grateful if u let me knowhow to embeed and where to embeed this please mail me soon i am in deep trouble

  19. dave Says:

    Hi Pratik,

    If its not showing up, and loading into another page, it either hasn’t attached the event or you may have another javascript error that is causing halt in any further javascript execution. Could you post your code here for me to take a look at>

  20. Mark Says:

    Get that man a pint! hell & even packet of walkers best! 30 seconds this problem took after your headsup rather than the 30 minutes it might have! Cheers.

  21. pratik Says:

    thanks a lot dave for replying that day i got the solution i have forgotten to add parent before this tb_init(’a.thickbox, area.thickbox, input.thickbox’); so the problem was coming now its working fine in ajax content thanks dave

  22. Rhiane Says:

    Wow, such a great article… i really helps me a lot….

    it solves my problem. i thought i could not solve it….

    THANK YOU VERY MUCH….. :D

  23. Leena Says:

    Cool it worked very fine. only Esc does not works

  24. Black Says:

    Thanks very much mate, you’re code here set off a massive lightbulb above my head.

  25. It’s Okay… I Figured It Out « MattGraham.ca Blog Says:

    […] after much pulling of hair and one finely tuned Google search, I found this page which gives the quick and dirty clean answer to how to get Thickbox to work with an AJAX page load. […]

  26. James Vec Says:

    You just saved me soo much time. If I had a hot sister I would hook you up

  27. paja Says:

    thnx.. i have googled so long :) great work ;)

  28. thijsvissia » Blog Archive » drawings added Says:

    […] On problem is that any images with thickbox popup links don’t work, because the events are attached when the page loads. So they have to be reattached. I found a solution here: http://www.djcnet.co.uk/jquery-thickbox-on-ajax-generated-pages.html […]

  29. thijs Says:

    This worked neatly. I am wondering if there is another solution using live events, since those are now supported by the core of jQuery. If that’s the case, perhaps the author of Thickbox can just change the way the events are attached to using live events, so that thickbox links dynamically loaded content get their events attached.
    The same goes for Gokce’s comment above: if you can call the init function for the newly added content, there’s no need to remove and reattach. (Still trying to make that work though but it sounds good.)

  30. Dominique Says:

    Thanks for this simple workaround, it works great !

  31. thijsvissia » Blog Archive » Read more right here Says:

    […] One problem is that any images with thickbox popup links that are in the ajax-retrieved part don’t work, because the javascript events are attached when the page loads. So they have to be reattached. I found a solution here: http://www.djcnet.co.uk/jquery-thickbox-on-ajax-generated-pages.html […]

  32. JC Says:

    Agreed, this was a life (and time) saver! There’s always something simple to a big problem :)

  33. Scott B Says:

    Thank you so much for this solution!

    Rolling it in with the “Transform” jQuery plugin solved one of my lingering functionality issues in a Thickbox AJAX modal (that loads jQuery UI and JWplayer+SWFobject… whew!)

  34. David Says:

    Thank you for this solution. For me the problem was dynamically adding to a list of thickbox thumbnail images, and the new images would not automatically work as thickbox popups, so the reference to tb_init was just the ticket, and saved a lot of messing around. Given that I’ve already been playing with LightBox, FaceBox and others to try to get a solution that works as I want it to, that’s a good thing.

  35. Mabuk Says:

    great help - cheers mate!

  36. Andre Says:

    When I add “bindThickBoxEvents();” as last line on the ajax refresh function then it doesn’t work.

    However, when I add “bindThickBoxEvents();”, to a seperate button which I click after the refresh occurred then it does work.

    I wonder why I need a separate button to call the “”bindThickBoxEvents();” function to make it work?

    Thanks.

  37. Andre Says:

    function refresh() {
    $(’#sites’).load(’lib/get_sites.cgi’);
    bindThickBoxEvents();
    }

    This doesn’t work. I can only get this to work if I call “bindThickBoxEvents();”, from for example a button-click after the refresh occurred.

  38. Andre Says:

    Solved it.. Duhh.. Have to wait till Ajax finished loading.
    $(’#boxfr’).load(’lib/get_sites.cgi?site=’, function(){bindThickBoxEvents();});

  39. Dan Says:

    Hey, thank you for your invaluable information…it did the trick!

  40. Nick Says:

    Spot on mate, thanks - was pulling my hair out for a bit there!

  41. Amol Bhavsar Says:

    This article is fantastic.
    You did more than half of my job.

    Thanks and regards,
    Amol A. Bhavsar.

  42. Amol Bhavsar Says:

    Hello there,
    I have a same problem.
    I need to use jQuery autocomplete plugin in AJAX generated content.
    Can you please help me?

  43. Dave Says:

    Thanks guys, can’t believe this is still of use, and how many people it has helped. I’ve been a bit lapse on writing new blog articles, I may focus in on jQuery, I write some more soon.

    Amol, what problem are you having with Autocomplete?

  44. Amol Bhavsar Says:

    I have to call jQuery autocomplete plugin in ajax generated page.
    It works in simple core PHP pages, but having problems in ajax generated pages.

    Your help on Thichbox was so amazing that I decided to use thickbox for my whole (Current) project and also for my future projects.

    Can you please reply ASAP.
    This is my personal request, I have to complete the task immediately.
    If possible, I request you to send replies on my Yahoo Mail ID.

    Expecting cooperation from you!

    Thanks and regards,
    Amol A. Bhavsar.

  45. Aaron Says:

    Your method also works with jquery-address. You need to add the tb_init(’a.thickbox, area.thickbox, input.thickbox’); part to the ajax part of your address.change function and it works like a dream. Vielen Dank!

  46. Amol Bhavsar Says:

    Thanks, it is working!

  47. nasgor Says:

    i had been trying to re-bind thickbox for a while… always had problem with it… thanks for your site it’s working fine now! many thanks!

  48. Suraj Baurai Says:

    Hi Dave,

    Could you please mail me whole bunch of code?

    I will really appreciate that and thanks for posting such a useful article.

    Thanks,
    Suraj

  49. Matt Richmond Says:

    Thanks for saving me some time!

  50. Alex92 Says:

    Libertarians such as Jon Arthur, Host Of Jon Arthur Live! ,

  51. JXL97 Says:

    Apparently I cannot tip the performers so that leaves it up to the developers to work out a payscale to bring in entertainment. ,

  52. Noor Man Says:

    great dude. i was having the issue of the popup window showing up twice. ur code fixed it. thanks.

  53. Rhiane Caña Says:

    Thanks Dave, it helps a lot. :)

Leave a Reply