Daniel Williams Daniel Williams - 7 months ago 22
Javascript Question

Getting the offsetWidth (or equivalent) of an LI element

So I'm trying to get the width of the element using jQuery.



alert($("#theList li:eq(0)").offsetWidth);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ol id="theList">
<li>The quick brown fox
</ol>




Answer

jQuery has $(elem).width() function that returns offsetWidth of element

alert($("#theList li:eq(0)").width());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ol id="theList">
  <li>The quick brown fox</li> <!--also you forgot closing tag -->
</ol>

Comments