Working on a simple calculator and a log. After getting a result after each calculation, a new record with this result appears in a log. Clicking on a circle in this log item, the circle should turn red, i.e.toggled. But the event is successful only in turn - the first circle gets red, the second doesn't, the third does and so on. Tried everything, dont't know what's happening. Please, help that's how it looks
const mainOperationsBlock = $('.main');
const numbersInput = $('#number-input');
const log = $('#log');
mainOperationsBlock.on('click', function() {
if (numbersInput.text() === '0' && $(this).text() === '0') {
numbersInput.text($(this).text());
} else if (numbersInput.text().match(/[-\+\*\/]$/) &&
$(this).text().match(/[-\+\*\/]/)) {
numbersInput.text(numbersInput.text().substring(0,
numbersInput.text().length - 1) + ''+ $(this).text());
} else if (/^0$/.test(numbersInput.text()) &&
/[123456789]/.test($(this).text())) {
numbersInput.text(numbersInput.text().substring(1,
numbersInput.text().length) + $(this).text());
} else {
numbersInput.text(numbersInput.text() + $(this).text());
}
});
$('#erase').on('click', function() {
numbersInput.text('0');
numbersInput.removeClass('error');
});
$('#result').on('click', getResult);
function getResult() {
let result = eval(numbersInput.text());
if(/[/]0/.test(numbersInput.text())) {
numbersInput.text('ERROR');
numbersInput.toggleClass('error');
} else {
$('#log').prepend(`
<div class='log-item'>
<span class='circle'></span>
<p class='equation'>${numbersInput.text()}=${result}
</p>
<span class='close'>✕</span>
</div>
` );
numbersInput.text(result);
let logItems = $('.equation');
logItems.each(function() {
if(/(48)/.test($(this).text())) {
$(this).css('text-decoration', 'underline');
}
});
$('.circle').on('click', function() {
$(this).toggleClass('red');
});
$('.close').on('click', function() {
$(this).parent().remove();
});
}
}
log.scroll(function() {
console.log(`Scroll Top: ${log.scrollTop()}`)
});