The variable selected
is a global one and accessed using this.selected
, which reliably returns the correct value.
However, when called from a dynamically created function, it returns unknown
.
onClick: function(val){
for (i = 0; i <
positions[this.selected].SHC.length; i++){
var tag =
positions[this.selected].SHC[i];
var tagEl =
document.createElement('p');
console.log(this.selected) // CORRECT VALUE DISPLAYED HERE
tagEl.onclick = function(){
console.log(this.selected) // RETURNS UNKNOWN HERE
for (j = 0; j < positions[this.selected].SHC.length; j++){
if (positions[this.selected].SHC[j] == this.innerHTML){
positions[this.selected].SHC.splice(j,1);
}
};
;}
tagEl.textContent = tag;
document.getElementById("uldshtags").appendChild(tagEl);
}
},
Is there a way to ensure that the global variable can also be accessed by dynamically created functions?