Skip to main content

Bug Tracker

Side navigation

#2453 closed bug (fixed)

Opened March 03, 2008 10:28PM UTC

Closed May 13, 2008 12:38AM UTC

attr(...) function returns undefined when value is 0

Reported by: gtong Owned by:
Priority: major Milestone: 1.2.4
Component: core Version: 1.2.3
Keywords: Cc:
Blocked by: Blocking:

The jQuery attribute function returns undefined if the value of the attribute is 0. The HTML code below returns undefined if you select "Foo", but 1 if you select "Bar"

<select id="foo" onChange="alert($('#foo').attr('selectedIndex'))">

<option value="Foo">Foo</option>

<option value="Bar" selected>Bar</option>


This is because of the following line (in jQuery 1.2.3 line # 178)

return this.length && jQuery[ type || "attr" ]( this[0], name ) || undefined;

The problem here is that the return value of the attr call is 0, which is evaluated as false, and thus passes to the || and returns undefined.

one possible way to fix this is:

if ( this.length ){

var ret = jQuery[ type || "attr" ]( this[0], name );

return ret === 0 ? 0 : (ret || undefined);

} else

return undefined;

Attachments (0)
Change History (1)

Changed May 13, 2008 12:38AM UTC by flesler comment:1

resolution: → fixed
status: newclosed

Fixed at [5574], check #2548.