3

1.4.4 return size of hidden element, but what about element in another hidden element? Is there any better solution then getWidth?

    <script type="text/javascript">
        function getWidth(element){
            var parent = element.parent();
            $('body').append(element);
            var width = element.width();
            parent.append(element);
            return width;
        }

        $(function(){
            var width = $("#foo").width(); 
            console.log(width); //0
            width = getWidth($("#foo"));
            console.log(width); //ok
        });
    </script>
    <div style='display:none'>
        <div style='display:none' id='foo'>bar</div>
    </div>
dotneter
  • 1,689
  • 2
  • 15
  • 24
  • 2
    see http://stackoverflow.com/questions/1841124/find-the-potential-width-of-a-hidden-element – Flo Dec 20 '10 at 09:14

1 Answers1

1

When elements are hidden, their width may vary depending on your styling. And when a hidden element is inside another hidden element, results could vary again. However, the Jquery code is quite straightforward:

$('#foo').parent().width()

This will grab foo's parent div, then grab its width, but I can't guarantee it will get the element's unhidden natural width. Hope that helps!

strongriley
  • 1,093
  • 1
  • 15
  • 12