The global variable selected is called like this.selected and always returns the correct value.
but when called from a dynamically created function its 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) // RETURNS CORRECT VALUE
                tagEl.onclick = function(){
                    console.log(this.selected) // RETURNS UNKNOWN
                    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);
            }
        },
How can I make the global variable also available for functions created dynamically.
 
    