Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: using live() with each()

Tags:

jquery

I have this function here:

$("a.fancybox_vid").each(function(){
   $(this).fancybox({
    titleShow     : false,
    width:    640,
    height:   395,
    autoDimensions: false,
    overlayOpacity: 0.6,
    href: "misc/mc.php?v="+$(this).attr('id')
  }); 
});

Now a link with c lass .fancybox_vid gets appended, and then this will not work. Only if it is there from the beginning. How can I have live() in this each().

like image 505
Karem Avatar asked Dec 19 '10 16:12

Karem


1 Answers

If you want "live-like" functionality for methods, you can use the livequery plugin:

$(function() {
    $('a.fancybox').livequery(function() {
        $(this).fancybox({
            titleShow     : false,
            width:    640,
            height:   395,
            autoDimensions: false,
            overlayOpacity: 0.6,
            href: "misc/mc.php?v="+$(this).attr('id')
        });
    });
});

...although it would be better (less overhead) to just call the fancybox plugin on the newly created elements.

like image 151
user113716 Avatar answered Oct 17 '22 09:10

user113716