Javascript DOM getElementById method of document object can also get the width of Div element using its onclick event. Likewise offsetHeight and clientHeight that we discussed in the previous article Javascript Get Height of Div onclick, there are two types of properties to get the width of <div> tag. offsetWidth and clientWidth are the properties that can be accessed using Javascript DOM method getElementById via document object.

Difference between offsetWidth and clientWidth property

Both offsetWidth and clientWidth properties have a difference in the value of returned as the height of the <div> tag:

offsetWidth: offsetWidth property of getElementById method gets the width of the div element that is specified in the CSS class style.

clientWidth: clientWidth property of the getElementById method gets the inner absolute width of the specified div element. That means if overflow CSS property of div element is defined with value scroll then clientWidth will return the width of the div approximately 16 pixels less than the offsetWidth i.e. value of CSS width property.

Javascript Syntax to get Width of Div

document.getElementById("id").clientWidth

document.getElementById("id").offsetWidth

Javascript Code to get Width of Div onclick

<html>
<head>
    <title>Javascript Get Div clientWidth and offsetWidth</title>
    <style type="text/css">
        #div1 {
            width: 200px;
            height: 100px;
            overflow: scroll;
        }
    </style>

    <script
        language="javascript"
        type="text/javascript">
    <![CDATA[
        function getDivWidth() {
            alert("Client Width:   " 
                  + document.getElementById("div1").clientWidth);

            alert("Offset Width:   " 
                  + document.getElementById("div1").offsetWidth);
        }
    ]]>
    </script>
</head>
<body>
    <center>
        <div id="div1">
            Get Div Width using Javscript function. Javascript DOM document can access the width
            of HTML Div element dynamically.
        </div>
        <br />
        <input
            type="button"
            value="Get Div Width"
            onclick="getDivWidth()" />
    </center>
</body>
</html>

Similarly you can get the height of the tag, see example here: Javascript Get Height of Div onclick. You can use both values i.e. height and width to dynamically expand the size of div tag.

Continue to next tutorial: Javascript Disable Input Text Selection to learn how to disable the selection feature on web page.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.