PRINTING ELEMENT WIDTH IN JAVASCRIPT

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
 1431  View(s)
Ratings:
Rate this:

I'm trying to retrieve an html element width in javascript. Nothing appears on screen

<head>
<script type="text/javascript">
    var width = $("#menu").width();
    document.write(width);
</script>
</head>
<body>
    <h1> Example </h1>
<ul id = "menu">
    <li><img src="img/Logo.jpg"/></li>
    <li><a href ="#">Home</a>
        <ul class = "sub1">
            <li><a href ="#">News</a></li>
        </ul>
    </li>
    <li><a href ="#">Machines</a>
        <ul class = "sub1">
            <li><a href ="#">Type1</a></li>
            <li><a href ="#">Type2</a></li>
            <li><a href ="#">Type3</a></li>
        </ul>
    </li>
    <li><a href ="#">Mission</a></li>
    <li><a href ="#">Contacts</a></li>
</ul>
<div id "main">
    <h2> Whatever </h2>
</div>
</body>
  1. Pravesh Singh

    Post:101

    Points:709
    Re: Printing element width in javascript

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <div id=head></div>
    <script type="text/javascript">
         $(function(){ // called when the DOM is ready
              var width = $("#menu").width();
              $('#head').html(width);
         });
    </script>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!