Bug Tracker

Modify

Ticket #2453 (closed bug: fixed)

Opened 5 years ago

Last modified 5 years ago

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

Description

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>

</select>

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 return ret === 0 ? 0 : (ret
"attr" ]( this[0], name );
undefined);

} else

return undefined;

Change History

comment:1 Changed 5 years ago by flesler

  • Status changed from new to closed
  • Resolution set to fixed

Fixed at [5574], check #2548.

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.