Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load scripts along with content in ajax

I am loading a set of images from another page using jquery load() function. I want the images that are loaded to be able to trigger the colorbox script but it is not working. I have all of the scripts on the page that triggers the load, but the script does not get applied to the fresh ajax content. How could I make this work?

Update

Thanks for the responses! It now works using the on function.. so I have something like this :

<a href="http://something.com" class="cbox"><img ... /></a>

        $j(".cbox").on( "click", function(){
              $j.fn.colorbox({ iframe:true,  scrolling: false, innerWidth: '650px', innerHeight: '390px' }); 
              return false;
              });

It seems to sort of work.. however now colorbox cant grab the href, it is returning that as undefined... but I guess that is another matter.

like image 720
Zac Avatar asked Oct 08 '22 21:10

Zac


1 Answers

Use jQuery's new .on() method, which replaces .live() and .delegate(). It works on elements that were added to the DOM after the code ran.

Had you posted some code, I'd have some to post back, but basically it looks like

$('imgselect').on('event',function(){
    //do stuff
});

event can be mouseenter, mouseleave, click, etc.

http://api.jquery.com/on/

like image 150
mowwwalker Avatar answered Oct 12 '22 02:10

mowwwalker