1$( "li" ).each(function( index ) {
2 console.log( index + ": " + $( this ).text() );
3});
4
1<!doctype html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>each demo</title>
6 <style>
7 div {
8 width: 40px;
9 height: 40px;
10 margin: 5px;
11 float: left;
12 border: 2px blue solid;
13 text-align: center;
14 }
15 span {
16 color: red;
17 }
18 </style>
19 <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
20</head>
21<body>
22
23<button>Change colors</button>
24<span></span>
25<div></div>
26<div></div>
27<div></div>
28<div></div>
29<div id="stop">Stop here</div>
30<div></div>
31<div></div>
32<div></div>
33
34<script>
35$( "button" ).click(function() {
36 $( "div" ).each(function( index, element ) {
37 // element == this
38 $( element ).css( "backgroundColor", "yellow" );
39 if ( $( this ).is( "#stop" ) ) {
40 $( "span" ).text( "Stopped at div index #" + index );
41 return false;
42 }
43 });
44});
45</script>
46
47</body>
48</html>
49