Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better way to jQuery set outerWidth(true) - width

In short, is there a way to set the outerWidth?

$.outerWidth(true, newValue)

Update: I found some addons that do this, although I need one function definition not 3 addons to do this.

like image 833
Hontoni Avatar asked Dec 17 '22 11:12

Hontoni


2 Answers

You were pretty close. With jQuery 1.8+, just use:

.outerWidth(newValue, true);

Also works for:

  • .innerWidth(val)
  • .innerHeight(val)
  • .outerWidth(val [, true])
  • .outerHeight(val [, true])

It actually became a setter with jQuery 1.8. That is 1 year after you posted your question. Moreover, it was listed in the changelog, but nobody updated the documentation which explains that it might have been missed by many.

If you have jQuery 1.7.2 or older, see Xeo's answer (on this page) or use jquery-ui 1.8.4+ which automatically enhances those methods into getters/setters (since in jQuery 1.8, those methods were actually ported from jquery-ui).

like image 122
jlgrall Avatar answered Dec 29 '22 00:12

jlgrall


Is this what you're looking for?

var a = $('element');
    actualWidth = a.outerWidth(true),
    desiredWidth = 1000,
    difference = desiredWidth - actualWidth,

a.css('width',difference);
like image 33
Xeo Avatar answered Dec 29 '22 00:12

Xeo