Ask Ben: Iterating Over An Array With jQuery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Looping Over Arrays With jQuery</title>
 
	<!-- Linked files. -->
	<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
	<script type="text/javascript">
 
		// Init the page once the DOM has loaded.
		$( InitPage );
 
 
		// Init the page.
		function InitPage(){
			// Get a refernce to the OL list element.
			var jList = $( "#list" );
 
			// Create our test array.
			var arrValues = [ "one", "two", "three" ];
 
			// Loop over each value in the array.
			$.each(
				arrValues,
				function( intIndex, objValue ){
 
					// Create a new LI HTML element out of the
					// current value (in the iteration) and then
					// add this value to the list.
					jList.append(
						$( "<li>" + objValue + "</li>" )
						);
				}
				);
 
		}
 
	</script>
</head>
<body>
 
	<h1>
		Looping Over Arrays With jQuery
	</h1>
 
	<p>
		Array Items:
	</p>
 
	<!---
		This is the list into which we will be storing
		the array items.
	--->
	<ol id="list">
		<!--- List items will be added dynamically. --->
	</ol>
 
</body>
</html>

For Cut-and-Paste