Bug Tracker

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#6610 closed bug (wontfix)

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.

Change History (7)

comment:1 Changed 9 years ago by addyosmani

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.

comment:2 Changed 8 years ago by addyosmani

Status: newopen

comment:3 Changed 8 years ago by addyosmani

Milestone: 1.4.31.4.4

comment:4 Changed 8 years ago by snover

Milestone: 1.4.41.4.5

Retargeting due to severity of regressions in 1.4.3.

comment:5 Changed 8 years ago by Bradley

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

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

comment:6 Changed 8 years ago by addyosmani

Keywords: needsdocs added
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.

comment:7 Changed 8 years ago by addyosmani

Keywords: needsdocs removed

Docs related to this item have now been updated.

Note: See TracTickets for help on using tickets.