Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace inner HTML of a div with Ajax response

i am trying to change inner HTML of a div after some interval. i am getting right response which i want with Ajax. but unable to replace inner HTML of selected after and with 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);
like image 864
Naresh Avatar asked Feb 13 '13 10:02

Naresh


2 Answers

this is the window in the callback. Use the value given to the callback of 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 don't need to define a new variable to protect this as jQuery already does it for you.

like image 89
Denys Séguret Avatar answered Oct 05 '22 19:10

Denys Séguret


As you are using an asynchronous function with a callback, this in your callback doesn't come from the same context. You need to save this in a variable used in the callback.

Try like 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);
like image 25
sdespont Avatar answered Oct 05 '22 19:10

sdespont