0

Am trying to display a delete icon when the mouse hovers over a div, but the hover event never works Here my code This function displays the divs with data

function load_pheeds()
    {
        var request_url = url+'pheeds/latest_pheeds';
        var timeline = $('#pheed-timeline');
        var loading = '<div class="progress-indicator">Loading Pheeds....</div>';
        timeline.append(loading);
        $.ajax({
            url:request_url,
            type:'GET',
            dataType:'json',
            error:function() { },
            success:function(data)
            {
                if(data.message == null)
                {
                    $('.progress-indicator').fadeOut('slow',function() {
                        $.each(data,function(index,pheed)
                        {
                            var del = '';
                            if(pheed.owner == "yes")
                            {
                                del = '<a href="#" class="deletePheed" style="display:none;">Delete Pheed</a>';
                            }
                            timeline.append(
                              '<div class="pheed-holder" data-id="'+pheed.pheed_id+'" data-delete="'+pheed.owner+'">'+
                              '<div class="user-profile-avatar"><img src="'+pheed.avatar_src+'" /></div>'+
                              '<div class="useridentity" data-userid="'+pheed.user_id+'">'+
                              '<a href="'+url+'users/info/'+pheed.username+'">'+pheed.username+'</a>'+
                              '</div>'+del+
                              '<div class="pheedContent">'+pheed.pheed+'</div>'+
                              '<div class="pheedMeta">'+
                              '<span class="timefield t:'+pheed.datetime+'000"></span>'+
                              '<span class="comment-count">'+pheed.comment_count+'</span>'+
                              '</div>'+
                              '</div>'
                            );
                        });
                    });
                }
            }
        });
    }

This is event handler

  $('div.pheed-holder').hover(function() {
            $(this).children('.deletePheed').show();
            },function() {
                $(this).children('.deletePheed').hide();
            });

The hover event never works

2
  • 1
    It works fine for me. Perhaps you are experiencing a JavaScript error somewhere along the line? Have you checked to make sure the anchor is actually being inserted into the HTML? Commented Nov 10, 2011 at 19:55
  • 1
    Have you tried $(this).find(".deletePheed") Commented Nov 10, 2011 at 19:58

3 Answers 3

1

I think your hover handlers get set before the element is appended? jQuery cannot add the handler if the div.pheed-holder doesn't exists on execution.

Try executing .hover(..., ...) after your ajax response; use live() instead.

Sign up to request clarification or add additional context in comments.

Comments

0

If the event's DOM object is dynamic like this, you need to use live (or on in 1.7) instead of bind.

$('div.pheed-holder').live("hover", function() {

You may have to bind with live to mouseenter and mouseleave individually instead of using hover.

Comments

0

Try to bind the event or use:

$('div.pheed-holder').live('hover',function(){});

1 Comment

live is depricated in 1.7 u can use on() instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.