Bug Tracker

Opened 11 years ago

Closed 11 years ago

#12271 closed feature (wontfix)

`.attr` and other such functions accepting a function returning an object

Reported by: anonymous Owned by:
Priority: low Milestone: None
Component: attributes Version: 1.8.0
Keywords: Cc:
Blocked by: Blocking:

Description

I would like to propose .attr (and other such functions) to accept a function as its only argument, which returns an object.

$("a").attr(function() {
  return {
    target: "_blank",

    // using an object directly wouldn't allow this line:
    href: $(this).data("foo")
  };
});

This is currently possible using .each. Given that functions can be passed to e.g. .attr when only one attribute is passed (.attr(str, func)), I think it would be useful to have this syntax available, too.

Thanks for taking this into consideration.

Change History (2)

comment:1 Changed 11 years ago by sindresorhus

Component: unfiledattributes
Priority: undecidedlow

comment:2 Changed 11 years ago by dmethvin

Resolution: wontfix
Status: newclosed

This can easily be done with other approaches such as $.each so I don't think we want to complicate the code with another signature.

Note: See TracTickets for help on using tickets.