What would be the correct way to reference "this.target" from within the function that is attached to an event.
Its a simple question I know, I have tried to search for answers here but my lack of knowledge is hampering me finding answers that are directly related to this problem.
function ChangeBgCol(target,color){
this.target = target;
this.color = color;
this.changeCol = function(){
document.getElementById("button").addEventListener("click",function(){
document.getElementById(this.target).style.backgroundColor = this.color;
});
}
}
var change = new ChangeBgCol("tar1","red");
change.changeCol();
.divStyle{
width: 50px;
height: 50px;
background-color: black;
<div class="divStyle" id="tar1"></div>
<div class="divStyle" id="tar2"></div>
<button id="button">press</button>