Retrieve the width of div element in JavaScript

By Faruque Ahamed Mollick

In many situations in web design or development, you may need to get the width of div element for some reasons. Here in this post, I am just going to show you simple code snippet which can be used to get the width of an HTML div element.

document.getElementById("divId").offsetWidth

The above JavaScript code will return you the width of the div element. It includes borders width also.

If you don’t want to include border width then go with the below code:

document.getElementById("divId").clientWidth

The above code will not include the border and take the width of a div element by excluding the border width.

Get the width of HTML div element using jQuery

If you want to retrieve the width of an HTML element using jQuery JavaScript framework then below is the given jQuery code which will do that:

$("div").width()

Below is given some more methods:

  • innerWidth() – Returns the width of an element (includes padding).
  • outerWidth() – Returns the width of an element (includes padding and border).

Leave a Reply

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