Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find width of div inside another div using JQuery

Hi I am trying to run this code that finds the width of div which is inside another div. but it is not running properly. How to do this?

i want the width of div mitem1

Code:

<!DOCTYPE html>
<html>
    <head>
    <style type="text/css">
        div#mydiv {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
    <script language="JavaScript">
        $('.menubar').each(function(menu){
            var len = $(this).find('.mitem').text().length;
            alert(len);
        });
    </script>
    </head>
    <body>

        <div class="menubar">

            <table border="1"><tr>
                <td><div class="mitem1">Home</div></td>
                <td><div class="mitem1">Communities<ul><li>item1</li><li>item2</li><li>item3</li><li>item4</li><li>item5</li></div></td>
                <td><div class="mitem1">Project Gallery</div></td>
                <td><div class="mitem1">For Our Customer</div></td>
                <td><div class="mitem1">Our Services</div></td>
            </tr></table>

        </div>

    </body>
    </html>
like image 847
milano Avatar asked Dec 07 '25 10:12

milano


1 Answers

$('.menubar .mitem1').each(function() {
  alert($(this).width());
});

Here's a fiddle

If you want to get the width, including the padding and border (and margin, optional), use outerWidth()

like image 53
billyonecan Avatar answered Dec 10 '25 01:12

billyonecan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!