Categories
JavaScript Answers

How to detect div dimension change with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *