Let's first take a look at JavaScript code =>
function ConstructNodes(className,hiddenId,alertMsg,formName){
this.className = className;
this.hiddenId = hiddenId;
this.alertMsg = alertMsg;
this.formName = formName;
}
ConstructNodes.prototype.getId = function(){
var nodes = document.getElementsByClassName(this.className);
for (var i=0;i<nodes.length;i++){
nodes[i].onclick = function(){
var r = confirm(this.alertMsg);
if (r==true){
alert(this.hiddenId); // undefined
} else {
return;
}
};
}
};
var obj = new ConstructNodes("className","hiddenId","Are you sure ?","formName");
obj.getId();
My problem in this situation is that defined objects are undefined under getId's anonymous function , how can I solve this situation ? thanks
function ConstructNodesis the classthisinside the event handlers set up by the "getId()" function to be the same as it is outside the handlers. It will not be.