Skip to main content

Bug Tracker

Side navigation

#6610 closed bug (wontfix)

Opened May 29, 2010 04:54AM UTC

Closed July 11, 2011 09:14PM UTC

Last modified July 13, 2011 11:25AM UTC

Inconsistent behavior setting dimensions with .height(val) and .width(val)

Reported by: Bradley Owned by:
Priority: low Milestone: 1.next
Component: dimensions Version: 1.4.2
Keywords: dimensions height width set value Cc:
Blocked by: Blocking:
Description

The API documentation for .height() http://api.jquery.com/height/ states:

"If no explicit unit was specified (like 'em' or '%') then 'px' is concatenated to the value."

This is only true when the type of the value passed is number. If the value is a string with no unit, jQuery does not set the unit to "px"; instead, it fails silently.

Lines 4570-4572 of 1.4.2:

if ( typeof value === "number" && !rexclude.test(name) ) {
	value += "px";
}

The first condition immediately fails if the value is a string. There appears to be no test for a string without a unit.

Some examples:

var h = 12.5;
// Does not work, since toFixed() returns a string
$(element).height( h.toFixed() );
// Works by appending a unit
$(element).height( h.toFixed() + 'px' );
// Works by converting to an number
$(element).height( h.toFixed() * 1 );

This apples to .width() as well.

Attachments (0)
Change History (7)

Changed September 30, 2010 10:01PM UTC by addyosmani comment:1

need: ReviewPatch
priority: → low

I've gone through a few test cases of using string-based values for height and width and it would indeed appear that where a type isn't specified, jQuery only appends the unit to 'px' where a number is provided. String based numbers (eg. "12.5") only work if you convert their type using something like parseInt() first and as originally mentioned, silently fail.

Updating to require a patch with low-priority.

Changed October 21, 2010 01:33AM UTC by addyosmani comment:2

status: newopen

Changed October 21, 2010 01:40AM UTC by addyosmani comment:3

milestone: 1.4.31.4.4

Changed October 21, 2010 10:57PM UTC by snover comment:4

milestone: 1.4.41.4.5

Retargeting due to severity of regressions in 1.4.3.

Changed October 26, 2010 05:56PM UTC by Bradley comment:5

Fiddle exhibiting bug here: http://jsfiddle.net/YkJuM/

Patch against 1.4.3 here: http://jquery.pastebin.com/1Cz74trr

Changed July 11, 2011 09:14PM UTC by addyosmani comment:6

keywords: dimensions height width set valueneedsdocs, dimensions height width set value
resolution: → wontfix
status: openclosed

Discussed this ticket with dmethvin and john: the official word is that if you pass a string in you'll need to pass the unit/px on the end yourself. If it's a number explicitly, we add the px to the end of it.

Understandably, this means that a minor docs change will need to be made however I'll take care of that.

Changed July 13, 2011 11:25AM UTC by addyosmani comment:7

keywords: needsdocs, dimensions height width set valuedimensions height width set value

Docs related to this item have now been updated.