Bug Tracker

Modify

Ticket #6610 (closed bug: wontfix)

Opened 3 years ago

Last modified 23 months ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by addyosmani

  • need changed from Review to Patch
  • Priority set to 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 3 years ago by addyosmani

  • Status changed from new to open

comment:3 Changed 3 years ago by addyosmani

  • Milestone changed from 1.4.3 to 1.4.4

comment:4 Changed 3 years ago by snover

  • Milestone changed from 1.4.4 to 1.4.5

Retargeting due to severity of regressions in 1.4.3.

comment:5 Changed 3 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 23 months ago by addyosmani

  • Keywords needsdocs, added
  • Status changed from open to closed
  • Resolution set to wontfix

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 23 months ago by addyosmani

  • Keywords needsdocs, removed

Docs related to this item have now been updated.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.