Skip to content
maxkfranz edited this page Mar 8, 2012 · 3 revisions

eles.each()

Call a callback function for each element in the collection.

eles.each( function(i, element) )

Calls the specified callback function on each element in the collection

function(i, element) : The callback function to execute for each element in the collection
i : The index of the element in the collection
element : The current element

Details

This function is useful for iterating over the elements in a collection. You can break early by returning false:

cy.nodes().each(function(i, ele){
  ele.data("foo", "bar");
  if( ele.selected() ) return false;
});

// this is the same
var nodes = cy.nodes();
for(var i = 0; i < nodes.size(); i++){
  var ele = nodes.eq(i);
  ele.data("foo", "bar");
  if( ele.selected ) break;
}

Examples

cy.nodes().each(function(i, element){
  console.log( "Looking at node `%s`", element.data("id") );
});

Clone this wiki locally