Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#7360 closed bug (wontfix)

.val(undefined) returns jQuery obj instead of value

Reported by: [email protected] Owned by:
Priority: undecided Milestone:
Component: attributes Version: 1.4.3
Keywords: regression Cc:
Blocked by: Blocking:

Description

In jQuery 1.4.3 the 'val' function behaves inconsistently if an undefined value is passed in explicitly. If you have a wrapper around this method that mirrors the "if param is undefined, return value" convention then it will no longer work due to 1.4.3 checking the number of parameters given, not the first param's value.

For example:

1.4.2


1341 val: function( value ) { 1342 if ( value === undefined ) { 1343 var elem = this[0];

item = $('input[type=text]'); item.val("foo"); value = undefined; item.val(undefined); returns "foo" item.val(value); returns "foo" item.val(); returns "foo"

1.4.3


1534 val: function( value ) { 1535 if ( !arguments.length ) { 1536 var elem = this[0];

item = $('input[type=text]'); item.val("foo"); value = undefined; item.val(undefined); returns [<input type="text"...>] item.val(value); returns [<input type="text"...>] item.val(); returns "foo"

Change History (2)

comment:1 Changed 11 years ago by snover

Component: unfiledattributes
Keywords: regression added
Milestone: 1.5
Resolution: wontfix
Status: newclosed

This is by design. See #4130.

comment:2 Changed 10 years ago by victor

As a consequence of this issue, the proper 'val' substitution should look as follows:

(function () {
    if (!jQuery || !jQuery.fn.val)
        return;

    var original = jQuery.fn.val;
    jQuery.fn.val = function (value) {

	... do something ...

        // jQuery ticket #7360 (http://bugs.jquery.com/ticket/7360)
        return typeof(value) !== "undefined" ? original.call(this, value) : original.call(this);
    };
})();
Note: See TracTickets for help on using tickets.