Royi Namir
Royi Namir

Reputation: 148544

jQuery outerWidth with set argument?

As I also faced the problem with jQuery's autocomplete incorrect width -

I saw this piece of code :

_resizeMenu: function() {
    var ul = this.menu.element;
    ul.outerWidth( Math.max(    //<------
        ul.width( "" ).outerWidth(),
        this.element.outerWidth()
    ) );
}

But AFAIK , outerWidth doesn't take parameter other than bool param to indicate margins.

I don't think they did this only to get truthy , falsy value , because they wouldn't need MAth.max in order to do it. they could use ||.

What am I missing ?

Upvotes: 4

Views: 705

Answers (1)

sroes
sroes

Reputation: 15053

See the source of jQuery:

// Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
    jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name }, function( defaultExtra, funcName ) {
        // margin is only for outerHeight, outerWidth
        jQuery.fn[ funcName ] = function( margin, value ) {
            var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
                extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );

            return jQuery.access( this, function( elem, type, value ) {
                var doc;

                if ( jQuery.isWindow( elem ) ) {
                    // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
                    // isn't a whole lot we can do. See pull request at this URL for discussion:
                    // https://github.com/jquery/jquery/pull/764
                    return elem.document.documentElement[ "client" + name ];
                }

                // Get document width or height
                if ( elem.nodeType === 9 ) {
                    doc = elem.documentElement;

                    // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
                    // unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
                    return Math.max(
                        elem.body[ "scroll" + name ], doc[ "scroll" + name ],
                        elem.body[ "offset" + name ], doc[ "offset" + name ],
                        doc[ "client" + name ]
                    );
                }

                return value === undefined ?
                    // Get width or height on the element, requesting but not forcing parseFloat
                    jQuery.css( elem, type, extra ) :

                    // Set width or height on the element
                    jQuery.style( elem, type, value, extra );
            }, type, chainable ? margin : undefined, chainable, null );
        };
    });
});

As you can see innerHeight, innerWidth, height, width, outerHeight and outerWidth are all created with the parameters margin and value.

Since the first parameter isn't a boolean (typeof margin !== "boolean"), chainable will be true, which in turn means the margin parameter will be used as the value when calling jQuery.acess (chainable ? margin : undefined).

This means it will call jQuery.style( elem, type, value, extra ) where type is "width", value is the result of Math.max and extra will be "border".

See for yourself what's happening by debugging jquery-1.10.2.js#9746 @ http://jsfiddle.net/95b7K/

Upvotes: 2

Related Questions