views:

36

answers:

2

Hi,

This is my jquery code

jQuery(document).ready (function() {

   // post
    $('.post').bind('mouseenter mouseleave', function() {
        $(this).filter('.btn').toggleClass('hidden');
    });


});

It works great on a normal document. but When I load some HTM: (i.e some divs with .post attributes) using ajax and embed it into my DOM.

The above code doesnt work with those divs.

+2  A: 

Your timing is probably off as the div with .post attributes must actually exist in the dom before you can bind anything to it.

edl
thanks for the knowledge. So what is the workaround ?
atif089
See if this helps you: http://api.jquery.com/delegate/ Use `$('body').delegate('.post','mouseenter mouseleave', f` etc
edl
I mean 'body' as your selector or whatever outer element your divs are contained in.
edl
yeah this is great thanks :) +1
atif089
+2  A: 

Try using live:

jQuery(document).ready (function() {
    $('.post').live('mouseenter mouseleave', function() {
        $(this).filter('.btn').toggleClass('hidden');
    });
});

Or better yet delegate

jQuery(document).ready (function() {
    $('#posts').delegate('.post','mouseenter mouseleave', function() {
        $(this).filter('.btn').toggleClass('hidden');
    });
});
PetersenDidIt
Hi, I tried this one and its not working both on static and ajax loaded content as well. And I want it to actually work with both the static content and the loaded content in one shot.
atif089
Live or delegate should make it so that the event fires for static and loaded content. There must be something else going on.
PetersenDidIt
No lock.. I dont know what it is.. But bind works fine with static content and live and delegate doesnt seems to be working
atif089
Are you sure the content you are loading through the ajax call has the `.post` class?
PetersenDidIt
My bad. I was on jquery 1.3.2 and it doesnt supported mouseover and mouseleave with live(). This works great ! Thanks
atif089