Skip to main content

Bug Tracker

Side navigation

#8770 closed enhancement (wontfix)

Opened April 05, 2011 11:30AM UTC

Closed April 07, 2011 02:37PM UTC

Last modified May 02, 2011 06:26AM UTC

Document `attr` behavior when new value function returns undefined

Reported by: tj@crowdersoftware.com Owned by:
Priority: low Milestone: 1.next
Component: attributes Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:
Description

''(Neither a bug nor a feature...let's go with enhancement.)''

When you call attr with a function, but then don't return a value out of the function (or return undefined), the attribute isn't changed. This is ''exactly'' the behavior I want for modifying the attribute only when it meets a certain condition, e.g.:

#!js
$("some_selector").attr("data-foo", function(index, value) {
    if (meetsCriteria(value)) {
        return updatedValue(value);
    }
});

Example: http://jsfiddle.net/cGtY5/

If it's intentional/desirable, could we document it (here)?

Attachments (0)
Change History (3)

Changed April 07, 2011 02:37PM UTC by timmywil comment:1

component: unfiledattributes
keywords: → needsdocs
priority: undecidedlow
resolution: → wontfix
status: newclosed

That seems like the right behavior to me. I think we could add something saying if nothing is returned in the setter function, the current value is returned since it acts like a getter when the value is undefined, or simply that there will not be any changes made if the setter function does not return something.

Changed May 02, 2011 05:58AM UTC by addyosmani comment:2

keywords: needsdocs

Changed May 02, 2011 06:26AM UTC by tj@crowdersoftware.com comment:3

Great to see the docs updated so quickly (some projects take ''months'' to make similar changes), but the wording seems awfully confusing. Perhaps simply:

Note: If the setter function doesn't return a value or returns undefined, the current value of the attribute is not changed. This can be useful for only changing the attribute when certain criteria are met in the setter function.