Replace internal HTML div file with Ajax response

I am trying to change the internal HTML div after some interval. I get the correct answer I want with Ajax. but cannot replace the internal HTML selected after and with the Ajax response. what is wrong with my code.

Html

      <p class="time ui-li-desc" data-time="2013-02-13 11:30:08" >
    51 seconds ago<img alt="image" src="images/read.png"></p>

      <p class="time ui-li-desc" data-time="2013-02-13 11:30:16" >
    58 seconds ago<img alt="image" src="images/read.png"></p>
.
.
.
.
.
      <p class="time ui-li-desc" data-time="2013-02-13 11:40:08" >
    10 minute ago<img alt="image" src="images/read.png"></p>

j query

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var sendTime=  $(this).attr("data-time");
                dataString = "sendtime="+sendTime+"&q=convertTime";
                $.ajax({
                    type: "POST",
                    url: "data_handler.php",
                    data: dataString,                   
                    cache: true,
                    success: function(response) {
                        alert(response);
                        $(this).html(response);
                        //alert(response);
                    }
                });
            });
        }, 5000);
+3
source share
4 answers

this- This is the window in the callback. Use the value specified for callbackfor each:

        $( ".time" ).each(function(index , elem) {
            var sendTime=  $(this).attr("data-time");
            dataString = "sendtime="+sendTime+"&q=convertTime";
            $.ajax({
                type: "POST",
                url: "data_handler.php",
                data: dataString,                   
                cache: true,
                success: function(response) {
                    alert(response);
                    $(elem).html(response);
                }
            });
        });

You do not need to define a new variable for protection this, since jQuery already does this for you.

+6
source

, this . this , .

:

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var sendTime=  $(this).attr("data-time");
                dataString = "sendtime="+sendTime+"&q=convertTime";
                var self = this;
                $.ajax({
                    type: "POST",
                    url: "data_handler.php",
                    data: dataString,                   
                    cache: true,
                    success: function(response) {
                        alert(response);
                        $(self).html(response);
                        //alert(response);
                    }
                });
            });
        }, 5000);
+4

I think $ (this) is out of context. Try:

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var $this = $(this);
                var sendTime=  $(this).attr("data-time");
                dataString = "sendtime="+sendTime+"&q=convertTime";
                $.ajax({
                    type: "POST",
                    url: "data_handler.php",
                    data: dataString,                   
                    cache: true,
                    success: function(response) {
                        alert(response);
                        $this.html(response);
                        //alert(response);
                    }
                });
            });
}, 5000);
+1
source
setInterval(function() { 
        $( ".time" ).each(function( index ) {
            var sendTime=  $(this).attr("data-time");
            var _thisvariable = $(this);
            dataString = "sendtime="+sendTime+"&q=convertTime";
            $.ajax({
                type: "POST",
                url: "data_handler.php",
                data: dataString,                   
                cache: true,
                success: function(response) {
                    alert(response);
                    _thisvariable.html(response);
                    //alert(response);
                }
            });
        });
    }, 5000);
+1
source

All Articles