Generally when you only want to clone some attributes but not data, events, etc.
Whenever you'd want to make a widget or something which turns an element into some more advanced UI, you'd like to not lose the attrs (id, class, title, etc.) which the user passed - but using $( elem ).clone() doesn't make sense...
A somewhat common problem: clearing a changed input:file - there's (that I know of) no way of doing it except to replace the input with a new one... you wouldn't want the attributes to get lost in the process - and $.clone might not be an alternative.
The enhancement would allow for simplified logic such as:
input.attr( textWrapper, 'title' + (type in { 'select': 1, 'radio': 1, 'checkbox': 1 } ? 'tabindex accesskey' : '') )
saving a lot of code-weight for conditionality-checking, etc.
Furthermore I think it's a good general practice for an "object" to copy from some object of same instance.
Also: I incorrectly wrote "value = list;" - it should be: "name = list;" and all references to "from" should be changed to "name"...
Updated version:
- attr: function( name, value, copyFilter ) {
- if ( name instanceof jQuery || name.nodeName && (name = $( name )) ) {
- var list = {};
- jQuery.each( value.split( ' ' ), function( val, key ) {
- list[key] = (val = name.attr( key )) && copyFilter && copyFilter[key] ? (val = copyFilter[key].call( val, key, val )) : val;
- });
- name = list;
- }
- return jQuery.access( this, name, value, true, jQuery.attr );
- },