0

Done a little work and I think I've found the answer...jQuery's live function.

But it leads to an issue. What I want to do is refresh the content of a div with id of container with new content. The original content had jQuery calls, so when the new content is loaded, I want it to be able to be called by jQuery as well (if that makes sense). Which is where I read jQuery's old .live function worked (by attaching stuff on page load and all times in the future until a true page refresh).

My jQuery is as follows:

        $(document).ready(function() {
            $('.fancybox').fancybox({
                width       : '1000px',
                height      : '1000px',
                afterShow   : function() {
                    $( ".fancybox-wrap" ).draggable();
                    }
            }); 


var sourceSwap = function () {
    var newSource = $(this).data('alt-src');
    $(this).data('alt-src', $(this).attr('src'));
    $(this).attr('src', newSource);
}
$(function () {
    $('img.xyz').hover(sourceSwap, sourceSwap);
});
    var originalContent = $('.player').html();
    var originalContent2 = $('.coords').html();
$('img.xyz').hover(function() {
var player = $(this).data('player');
var coords = $(this).data('coords');
     $('.player').html('<strong>'+ player +'</strong>');
     $('.coords').html('<strong>'+ coords +'</strong>');
}, function() {
     $('.player').html(originalContent);
     $('.coords').html(originalContent2);
});
 });
var centerX = <?=$_GET['x']?>;
var centerY = <?=$_GET['y']?>;

$(function(){
var hor = 0;
var vert = 0;
$('#left').click(function () {
    scroll = $('#container').scrollLeft();
    $('#container').animate({'scrollLeft': scroll-300},1000);
    centerX = centerX - 5;
    hor--;
    if(hor <= -3){mapRefresh();}
});
$('#right').click(function () {
    scroll = $('#container').scrollLeft();
    $('#container').animate({'scrollLeft': scroll+300},1000);
    centerX = centerX + 5;
    hor++;
    if(hor >= 3){mapRefresh();}
});
$('#up').click(function () {
    scroll = $('#container').scrollTop();
    $('#container').animate({'scrollTop': scroll-300},1000);
    centerY = centerY - 5;
    vert++;
    console.log(vert, " vert");
    if(vert >= 3){mapRefresh();}
});
$('#down').click(function () {
    scroll = $('#container').scrollTop();
    $('#container').animate({'scrollTop': scroll+300},1000);
    centerY = centerY + 5;
    vert--;
    if(vert <= -3){mapRefresh();}
});
<? $totalHeight = 60 * 42;
$totalWidth = 60 * 42;?>
$('#container').scrollTop((<?=$totalHeight?>-$('#container').height())/2).scrollLeft((<?=$totalWidth?>-$('#container').width())/2);
});


function mapRefresh() {
    $.ajax({
       type     : "POST",
       cache    : false,
       url      : "map2.php?map=refresh",
       data     : {'X': centerX, 'Y': centerY},
       success  : function(data) {
                    $("#container").html(data); 
                  }
      });

}

The stuff like $('#down').click(function () { I believe is simple to replace to $('#down').on( "click", function() {, but what of var sourceSwap = function () { or $(function () {? Those aren't "clicks" or "scrolls" or anything...those are just there, and need to be called on load, and then what of $('.fancybox').fancybox({?

I am new to javascript/jQuery and confused. I've figured the above out, but the only thing I can think of doing is including all the jQuery in the div id container, and replacing it with an exact copy of itself so that it re-runs, but then that seems to cache both the original running and the second running of the jQuery, so that it will run two ajax requests simultaneously, and then three at once, and then four at once, etc. the more times up/down/left/right are clicked.

Witold Kowelski
  • 924
  • 2
  • 12
  • 27
  • You will probably want to use delegated event handling. You can read about it in these answers: [.live() vs. on()](http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht/8752376#8752376) and [jQuery .on does not work but .live does](http://stackoverflow.com/questions/9985090/jquery-on-does-not-work-but-live-does/9985137#9985137) and http://stackoverflow.com/questions/9814298/does-jquery-on-work-for-elements-that-are-added-after-the-event-handler-is-cre/9814409#9814409. – jfriend00 Apr 12 '14 at 02:47
  • You will need to use the dynamic form of `.on()` which is like this: `$("static parent selector").on("click", "dynamic element selector", function() {});` – jfriend00 Apr 12 '14 at 02:51
  • If I understand you correctly, you want to have your ajax call return content that contains javascript code that needs to run. My advice would be to see whether you really need to return the javascript with your content vs. restructuring it so that all your handlers/code are in the main page and your ajax call just gets actual content. If you absolutely must return and execute js in the returned content, then you will want to check out this question: http://stackoverflow.com/questions/4619668/executing-script-inside-div-retrieved-by-ajax. – patmortech Apr 12 '14 at 02:53
  • The code that container contains and gets replaced with is similar to the following `` (just hundreds of them with different coords, images, etc). When the div is refreshed, it needs to continue to allow for the src and alt-src to be swapped, it needs for the coords and player name to be taken and displayed in – Witold Kowelski Apr 12 '14 at 05:22
  • a table, and I think that's it. All of that works on the initial page load (all the jQuery is there). It's just that when the div is refreshed, it no longer works. What was working was for me to include all the jQuery in the div and when the div was refreshed, just refresh the jQuery with the rest of the div content, but that becomes a bandwidth hog and the variables vert and hor do not get reset to 0. Which is why I'm thinking .on might work since .live attaches an event handler for all elements which match the current selector, now and in the future, and .on replaced .live – Witold Kowelski Apr 12 '14 at 05:28

0 Answers0