1

I have a several divs that a refreshed using Ajax after the user clicks on a link on the page.

Here's my code for the Ajax refresh div:

<script type="text/javascript"><!-- AJAX CALL FOR MY PICTURES -->
$(document).ready(function() {
$('#pictures_wrapper a.delete_image').live('click', function(e){
     $('#pictures_wrapper').load( $(this).attr('href') + ' #pictures_wrapper' );     
     e.preventDefault(); 
}); 
});
</script>

On this same page, I am loading this Jquery effect:

<script type="text/javascript"><!-- HOVER OVER ITEMS CHANGE BG COLOR -->
$(document).ready(function() {
$(".item_control_box").hide();
    jQuery('.each_item_container').hover(function() {
        jQuery(this).find('.item_control_box').show()
    }, function() {
        jQuery(this).find('.item_control_box').hide();
    });
});
</script>

My issue is that when the user click on the link to refresh the Divs, this jquery effect breaks and no longer works. I'm thinking that I have to "reload" this function some how since only the div is being reloaded and not the whole page. How can I keep this Jquery function working after refreshing a div?

1
  • 1
    seems like you have to use live on hover event too. Commented Nov 8, 2011 at 23:15

4 Answers 4

1

You should use the callback function of the load, means u recall the jquery after loading has completed.

$('#pictures_wrapper').load( $(this).attr('href') + ' #pictures_wrapper', , function () { //hover effect function } );

Hope this help :)

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

1 Comment

hi, please refer to api.jquery.com/load The code here i just showing as your reference, I dint check for the syntax, but I solve similar situation by that :)
1

Your code should work if you change your .bind() calls to .live():

jQuery('.each_item_container').live('mouseenter', function() {
    jQuery(this).find('.item_control_box').show()
}).live('mouseleave', function() {
    jQuery(this).find('.item_control_box').hide();
});

.hover() is the same thing as using .bind('mouseover', function () {...}) and .bind('mouseout', function () {...})

When you want to bind event handlers to elements not yet in the DOM you can use .live() or .delegate(): http://api.jquery.com/live/, http://api.jquery.com/delegate/

Attach an event handler for all elements which match the current selector, now and in the future.

As of jQuery 1.7 you should be using .on() as .bind(), .live(), and .delegate() are depreciated: http://api.jquery.com/on/

1 Comment

I think this is the route I need to take, but when I updated my code, and I hover over the div, it doesn't show .item_control_box
1

Better put the code in a custom function with a name

function effectLoad(){

$(".item_control_box").hide();
    jQuery('.each_item_container').hover(function() {
        jQuery(this).find('.item_control_box').show()
    }, function() {
        jQuery(this).find('.item_control_box').hide();
    });

}

then put the function in your ajax success function

 $.ajax({
            url: 'your_php_file.php',
            type: 'POST',
            data:formData,
            success: function(response)
            {

                $("#formbox").html(response);

                  effectLoad();

            }

        });

Hope it will work.

Thanks

Comments

0

as @Guillaume Cisco mentioned in the comments you have to rebind the hover effect to dynamically inserted elements in the DOM, you can do it like

$(".item_control_box").live(
{
mouseenter:function(){
jQuery(this).find('.item_control_box').show();
},
mouseleave:function(){
jQuery(this).find('.item_control_box').hide();
}
});

Comments

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.