Hello! I need help with a piece of JavaScript code below:
-This code runs perfectly when executed (together with accompanying HTML and CSS files).
let aFigure = document.querySelectorAll('figure');
aFigure.forEach(element => {
element.onclick = function(){
element.style.width = '70%';
element.style.height = 'auto';
}
});
-These code blocks execute the function as soon as HTML is loaded, before the ‘click’ event, although both are of the same logic as the first.
1:
function boxAdjuster (box) {
box.style.width = '70%';
box.style.height = 'auto';
}
let aFigure = document.querySelectorAll('figure');
aFigure.forEach(element => { boxAdjuster(element) });
2:
function boxAdjuster (box) {
box.style.width = '70%';
box.style.height = 'auto';
}
let aFigure = document.querySelectorAll('figure');
aFigure.forEach(element => {
element.addEventListener('click', boxAdjuster(element))
})
Please highlight/clarify the mistake in the code or code logic. Thanks!