My slider output which reads the value given/updated by user using oninput event handler does not update correctly when I call using
slider.oninput = updateGridRange();
syntax with parenthesis, rather it gives me the same default value of 16x16. However the code works great without that bug when I call using
slider.oninput = updateGridRange;
What is the difference in JavaScript when I call a function with no parameters defined functionExample()
and functionExample
without parentheses? Why is there a bug created in my code when this situation happens?
function createSlider() {
const slider = document.createElement('input');
slider.setAttribute('type', 'range');
slider.setAttribute('min', '1');
slider.setAttribute('max', '100');
slider.setAttribute('value', '16');
document.querySelector('div.slider-container').appendChild(slider);
const sliderText = document.createElement('div');
sliderText.classList.add('slider-text');
document.querySelector('div.slider-container').insertBefore(sliderText, document.querySelector('div.slider-container').firstChild);
sliderText.textContent = `${slider.value} x ${slider.value}`;
slider.oninput = updateGridRange; //updateGridRange() causes bug
}
function updateGridRange() {
document.querySelector('div.slider-container div.slider-text').textContent = `${document.querySelector('input').value} x ${document.querySelector('input').value}`;
}
Pictures of Slider
Bug
Good