Replace inner HTML of a div with Ajax response

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.


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);