lgt
lgt

Reputation: 1034

jquery won't refire after ajax reload

I'm having the following jquery code what I'm using to tooltip images and fire ajax call requests. Till a point is working fine, but after the div is reloaded by ajax the tooltip and slider what actually should be able to start a new ajax call is stopping to work.

// Load this script once the document is ready
$(document).ready(function () {

 // Get all the thumbnail
 $('div.thumbnail-item').mouseenter(function(e) {

  // Calculate the position of the image tooltip
  x = e.pageX - $(this).offset().left;
  y = e.pageY - $(this).offset().top;

  // Set the z-index of the current item, 
  // make sure it's greater than the rest of thumbnail items
  // Set the position and display the image tooltip
  $(this).css('z-index','15')
  .children("div.tooltip")
  .css({'top': y + 10,'left': x + 20,'display':'block'});

 }).mousemove(function(e) {

  // Calculate the position of the image tooltip   
  x = e.pageX - $(this).offset().left;
  y = e.pageY - $(this).offset().top;

  // This line causes the tooltip will follow the mouse pointer
  $(this).children("div.tooltip").css({'top': y + 10,'left': x + 20});

 }).mouseleave(function() {

  // Reset the z-index and hide the image tooltip 
  $(this).css('z-index','1')
  .children("div.tooltip")
  .animate({"opacity": "hide"}, "fast");
 });



$(function() {
    $( ".slider" ).slider({
        value:0,
        min: 0,
        max: 100,
        step: 10,
        change:function(e,ui){

            var domain = document.domain;
            var count = $('#amount').val();
            var $parent = $(this).closest(".product_box");
            var modul_title = $("h4", $parent).text();
            alert(count);
            $.ajax({

                url:'index/ajax',
                data:{mod_title:modul_title, domain:domain, count:count},
                cache:true,
                datatype:'html',
                success: function(response) {
                     if(response.status = modul_title) {
                         $parent.fadeOut();
                         $parent.html(response).fadeIn(); 

                     } else 

                     {
                          alert("something went wrong!");
                     }

                   }

                });

        },
        slide: function( event, ui ) {
            $( "#amount" ).val(ui.value );
        }



    });
    $( "#amount" ).val( $( "#slider" ).slider( "value" ) );
});

});

Upvotes: 0

Views: 593

Answers (1)

Fluidbyte
Fluidbyte

Reputation: 5210

If an object is being created dynamically you will need to use something like .delegate():

http://api.jquery.com/delegate/

or, better yet, the newer .on method:

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

Then bind the events and it should work. For example:

 $('div.thumbnail-item').on('mouseenter',function(e){ ...

Upvotes: 2

Related Questions