jQuery Tip: How to Break Out of an .each() Loop

Here’s a nice little jQuery tip I found today.  If you want to break out of an each loop in jQuery, you can do it by returning false.

Here’s the real example from $().SPServices.SPArrangeChoices:

		$("td.ms-formbody").each(function() {
			alert($(this).html());
			if(searchText.test($(this).html())) {
				var radios = "<TR>";
				var choiceCount = 0;
				$(this).find("tr").each(function() {
					radios += $(this).html();
					choiceCount++;
					if(choiceCount % opt.perRow == 0) radios += "</TR><TR>"
				});
				radios += "</TR>";
				$(this).find("tr").remove();
				$(this).find("table").append(radios);
				return false;
			}
		});

Tip ‘o the tam ‘o shanter to crad on tumblr for this one.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.