dllhell dllhell - 1 year ago 71
jQuery Question

How to bind newly added HTML to Colorbox after Document Ready

I'm rendering a gallery (or one picture) on server side and getting HTML like this:

<ul class='cs-style-3' >
<img height='100px' class='gallery' src='someurl'>
<div imgtitle='sometitle' class='cgallery' href='someurl'></div>


$(document).ready(function ($) {

$(".cgallery").colorbox({ rel: 'gallery', title: function () { return $(this).attr('imgtitle'); } });


HTML is received through ajax call and appended to page:


Problem is that HTML is received after document.ready and by clicking on

<div imgtitle='sometitle' class='cgallery' href='someurl'></div>

Colorbox simply doesn't work. When this HTML is placed on page before document ready, everything works fine, so, code is ok.

How to make colorbox work when related HTML is added after document ready?

Answer Source

re-initiate colorbox, once the html is rendered. Basically inside ajax callback function.

          rel: 'gallery', 
          title: function () { 
                 return $(this).attr('imgtitle'); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download