g(..).iterate(callbackFn)

Iterates through the Gimme Object's internal array, and for each element, executs the function, callbackFn.

Returns: Itself (the same Gimme Object is returned so additional methods can be chained together seamlessly).
Params:
  • callbackFn, required: A function reference that will be invoked for each element in the Gimme Object's internal array.

Examples:
// alerts the id and position in the DOM of all <div> elements with the class "foo"
g('div.foo').iterate(function(i)
{
	alert(i + ': ' + this.element().id);
});

// removes every other <p> element from the DOM, while highlighting all others with a red border
g('p').iterate(function(i)
{
	var el = this.element();
	if (i % 2 === 0)
	{
		el.parentNode.removeChild(el);
	}
	else
	{
		this.setStyle('border', '3px solid red');
	}
});



Related Information:
  • The loop index, i, is automatically passed into the callbackFn.
  • Inside the callbackFn, the 'this' keyword is, itself, a Gimme Object wrapping the DOM element being accessed by the current iteration, which explains the need to invoke .element() to access the underlying DOM element directly.

Last edited Jul 9, 2007 at 10:46 PM by sstchur, version 3

Comments

No comments yet.