Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this function I want to use that will cycle through all the DIVs using .each in a page that have a certain class, then the DIV get replaced by the results of a .get. Here is the code:
PHP
$(function(){
    window.setInterval(function() {
    $(".cstat").each(function (i) {
        $.get("getcustomerstatus.asp?customerid="+this.id).done(function(data){$("#"+this.id).html(data);});
        })
    }, 10000);
})


The problem is that in the .done the "this.id" I believe gets evaluated at the return of the data from the .get. My goal is to replace all the DIV. So my overall question is there a way to make the this.id in the .done section get "inserted" at the time of the .get instead of the .done?

Thanks
Posted

1 solution

You can store that id of div before calling that .get() function like below.
JavaScript
$(function(){
    window.setInterval(function() {
    $(".cstat").each(function (i) {
        var currentDivId = this.id;

        $.get("getcustomerstatus.asp?customerid="+this.id).done(function(data){$("#" + currentDivId ).html(data);});
        })
    }, 10000);
})

Let me know it solves your problem or not.
 
Share this answer
 
Comments
onemorecoke 28-May-13 13:53pm    
Worked perfect! I am curious about why this fixed it. I could see the usefulness of both ways but wondered what triggered the "current" replacement vs the "later" replacement. Anyway, maybe for another day...thank you again
Most Welcome buddy... :)

The keyword "this" always refers to the current object/element.
As you were calling it inside that "done()", so it will refer that object not the div.
So, I stored it in a variable to use that inside the function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900