Bug Tracker

Opened 13 years ago

Closed 13 years ago

#1797 closed feature (wontfix)

Integrate extend-like Behavior in each

Reported by: j5 Owned by:
Priority: trivial Milestone: 1.2.2
Component: core Version: 1.2.1
Keywords: each DOM Cc:
Blocked by: Blocking:

Description

Would like each method when passed an object instead of a function to merge the passed object onto the DOM element.

example:

jQuery("#somediv").each({innerHTML: "blah blah blah"});

This would give a shortcut to manipulating non-attribute fields of DOM objects (such as references to other DOM objects which have been attached).

I have modified jQuery-1.2.1.js each as follows to accomplish this:

	// args is for internal usage only
	each: function( obj, fn, args ) {
		if ( args ) {
			if ( obj.length == undefined )
				for ( var i in obj )
					fn.apply( obj[i], args );
			else
				for ( var i = 0, ol = obj.length; i < ol; i++ )
					if ( fn.apply( obj[i], args ) === false ) break;

		// A special, fast, case for the most common use of each
		} else {
			if ( obj.length == undefined )
				// modified to extend each to do an extend on the object returned with object passed
				for ( var i in obj ) {
					if (typeof fn == 'function')
						fn.call( obj[i], i, obj[i] );
					else if (typeof fn == 'object')
						for (var f in fn)
							obj[i][f]=fn[f];
				}
			else
				if (typeof fn == 'function')
					for ( var i = 0, ol = obj.length, val = obj[0]; 
						i < ol && fn.call(val,i,val) !== false; val = obj[++i] ){}
				else if (typeof fn == 'object')
					for ( var i = 0, ol = obj.length; i < ol; i++)
						for (var f in fn)
							obj[i][f]=fn[f];
		}

		return obj;
	},

Change History (1)

comment:1 Changed 13 years ago by john

Resolution: wontfix
Status: newclosed
Type: bugfeature

I don't understand the logic of this addition. Why not just use .attr()?

Note: See TracTickets for help on using tickets.