[jQuery] Dynamically added "click" registration always returns the last value added

[jQuery] Dynamically added "click" registration always returns the last value added


Hello,
I'm parsing this JSON:
{"result":"success","successText":"Your changes have been saved
successfully.","errorText":"","memberID":"49","mode":"edit","jobs":[{"jobID":"1","companyName":"SAIC"},{"jobID":"2","companyName":"Aspentech"},{"jobID":"3","companyName":"Cardinal
Health"}]}
...with this Javascript:
$("#panelManageJobView").empty();
                
for (var i in json.jobs)
{
    var j = json.jobs[i];
    var div = "<div id='job" + j.jobID + "'><h1 id='jobTitle" + j.jobID + "'
style='display:inline;font-weight:normal'>" + j.companyName + "</h1>&nbsp;|
# edit ";
    div += "<div style='padding-top:10px;display:none' id='jobEdit" + j.jobID +
"'></div></div>";
    if (i > 0)
    {
        div = "<div class='padLine'>&nbsp;</div>" + div;
    }
    $("#panelManageJobView").append(div);
                    
    //now register click event on edit link
    $("#editJob" + j.jobID).click(function() {
        alert(j.jobID);
    });
}
Everything works fine, and is added to the page correctly. I even viewed
the generated HTML and confirmed that the ID's are correct on each div that
gets appended. However, when I click on the "edit" link next to each item,
the alert always shows the last "jobID" that was received in the JSON - "3"
in this example. I'm expecting to get "1" if I click the first edit link,
"2" if I click the second, and so on.
Any ideas what I'm doing wrong?
Thanks,
Chad
--
View this message in context: http://www.nabble.com/Dynamically-added-%22click%22-registration-always-returns-the-last-value-added-tf2812575.html#a7848746
Sent from the JQuery mailing list archive at Nabble.com.
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/