Sometimes, we want to detect div dimension change with JavaScript./
In this article, we’ll look at how to detect div dimension change with JavaScript.
How to detect div dimension change with JavaScript?
To detect div dimension change with JavaScript, we use ResizeObserver.
For instance, we write
const outputSize = () => {
console.log(textbox.offsetWidth, textbox.offsetHeight);
};
outputsize();
new ResizeObserver(outputSize).observe(textbox);
to create a ResizeObserver obbject with the outputSize function.
And we call observer with the textbox element to watch its size change.
We get the width and height with offsetWidth and offsetHeight.
Conclusion
To detect div dimension change with JavaScript, we use ResizeObserver.