Use 'return' to break out of each() loops early.
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function () {
$("div").each(function (index, domEle) {
$(domEle).css("backgroundColor", "yellow");
if ($(this).is("#stop")) {
return false;
}
});
});
});
</script>
</head>
<body>
<body>
<button>Change colors</button>
<span></span>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div id="stop">Stop here</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
</body>
</html>
Related examples in the same category