views:

162

answers:

3

Can you use the .width() and .height() methods to increase the height like $('x').width('+=2px') or $('x').height('+=2px')?

I couldn't find any information about this on the jQuery website, but I also haven't seen anything to suggest that this doesn't work.

+2  A: 

Why not just do something like this?

var el = $('x');
el.width(el.width()+2);

EDIT: To clean it up, you can create a plugin, something like this (untested):

jQuery.fn.increaseWidth = function(amount) {
  this.width(this.width()+amount);
  return this;
};

This would allow you to do $('x').increaseWidth(2)

Kyle Slattery
I mean, I knew you could do this, but it's really clunky and I thought there could be a shorter way to do this.
Alexsander Akers
If you wanted to do it the way you're talking of, you could write a plugin to make a `$('x').increaseWidth(2)` function.
Kyle Slattery
+1  A: 

No. The value you pass to width() is expected to be a CSS length value. Pixels are assumed if you don't specify a unit.

Sixten Otto
+1  A: 

These methods get and set the height or width of the elements.

width() or width(val)

Such approach as you say is only provided on .animate()

Sinan.

Sinan Y.
Thanks for the distinction!
Alexsander Akers