Wednesday, 25 July 2012

How to break out of jQuery.each()?

JavaScript break statement doesn't work with jQuery.each(). But the same functionality can be achieved by adding a "return false" statement within jQuery.each() once the breaking condition is reached.

$(‘ul li’).each(function(index){
  if ( $(this).hasClass(‘selected’) )
  {
    selected = index;
    return false; 
  } 
});

No comments:

Post a Comment