I have a variable named tableIndexNumber that I need to use in different methods. When trying to access this variable, I use "this.tableIndexNumber" and it works fine. However, I face an issue when using it inside a jQuery each loop because the HTML elements are also defined as "this" within the loop. What should be my approach in this situation?
export class TablesComponent implements OnInit {
tableIndexNumber = 1;
constructor() { }
change(col: number) {
$('#' + col).each(function () {
if ($(this).attr('class') === 'box') {
$(this).addClass('boxSelected');
// here is where the problem occurs with this.tableIndexNumber ++
} else {
$(this).removeClass('boxSelected').addClass('box');
}
});
}