PDA

View Full Version : variable inside a dynamically created function



ellisd5
01-20-2010, 12:59 PM
Hi all,

I have a set or icons that which over time I replace with a different icon and and add a onclick event to them. This is my code


function updateStatusIcons(retText) {
updatingStatuses = true;
var updates = retText.split("|");

for (z=0; z<updates.length; z++) {
var sysId = trim(updates[z].split(":")[0]);
var projectId = trim(updates[z].split(":")[1]);

if (projectId != "") {
var statusImg = getSysidsImg(sysId);
statusImg.src = "images/tick.gif";
statusImg.className = "hand";
statusImg.title = "Project "+ projectId +" created, Click here to find in project search";
statusImg.onclick = function () { document.location = "projectSearch-FTTC.jsp?initAction=loadproject&projectId="+ projectId; };

// Remove sysid from csv
removeSysidfFromCSL(sysId);
}
}


if (pollingOn) {
pollingupdate = setTimeout("checkForProjectComplete()", 5000);
}
updatingStatuses = false;

if (runErrorFunction) {
markErrors();
}
}


Everything works fine except for the onclick event. In the hover message of the icon, the correct project ID is displayed in the message, hoever in the function, the onclick funtion always loads the page with the last set project id. How can I pass the project id into the onclick function and make it stay fixed and not be the value of that it was last set to?

TIA,
Dale

abduraooft
01-20-2010, 01:32 PM
Try
if (projectId != "") {
var statusImg = getSysidsImg(sysId);
statusImg.src = "images/tick.gif";
statusImg.className = "hand";
statusImg.fauxId=projectId;
statusImg.title = "Project "+ projectId +" created, Click here to find in project search";
statusImg.onclick = function () { document.location = "projectSearch-FTTC.jsp?initAction=loadproject&projectId="+ this.fauxId; };

// Remove sysid from csv
removeSysidfFromCSL(sysId);
}

ellisd5
01-20-2010, 02:43 PM
Ok cool, that worked.

That did cross my mind setting an attribute but believe I have read that you can't set fake attributes in some browsers.

Anyway, don't know if that's try and I only have to support FF and IE and it works fine so i'm happy.

Thanks,
Dale