Jquery loop through items separately

Jquery loop through items separately

Hi,
 
I want to make a for loop with jQuery to reduce code, but it wouldn't work.
This is the sorth of thing I want to do:
run through an array of <li> and animate them separately with a pause in between.
  1. $('#thebutton').click(function(){
      var items = $ ( '#menu > li' );
         $ (items [ 0 ] ). delay ( ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
         $ (items [ 1 ] ). delay ( 200 ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
         $ (items [ 2 ] ). delay ( 400 ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
         $ (items [ 3 ] ). delay ( 600 ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
         $ (items [ 4 ] ). delay ( 800 ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
         $ (items [ 5 ] ). delay ( 1000 ). animate ( { "opacity": "1", "marginTop": "2px" }, 400 );
     $ ( '#thebutton' ). animate ( { "opacity": "0", "marginTop": "-300px" }, 500 );
      } );

I tried it like the code beneath, but it animated all the <li> tags at once, instead of each delayed... :s
  1. $('#thebutton').click(function(){
  2.   var items = $ ( '#menu > li' );
      for (i=0;i<=items.length;i++){
  3.      $ (items [i ] ). delay ( 300 ). animate ( { "opacity": "1", "marginTop": "2px" }, 1700 ); }
     $('#thebutton').animate({ "opacity": "0", "marginTop": "-300px" }, 500);
    });

Right here, the rest of the html, if you need it:

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
      <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
       
          <title>JQuery </title>
       
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"> </script>
       
      <script type="text/javascript" >
    2. // right here, put the javascript I discussed
    3. </script>
    4. <style>
       
      li, ul, body, p, html { margin: 0px; padding: 0px; }
       
      body { font-family: Verdana, Arial; font-size: 3mm; }
       
      ul { width: 100px; margin: 50px; background-color: 999999; }
       
      li { display: inline; float: left;
      width: 50px; margin: 1px; padding: 2px;
      border: solid 1px #333333; background-color: #666666; text-align: center;
      }
       
      #thebutton { width: 50px; margin: 5px; border: solid 1px #333333; padding: 2px; background-color: #666666; text-align: center; }
      #menu { position: absolute; top: 0px; left: 0px; }
       
      .breek { display: block; clear: left; }
       
      </style>
       
      </head>
      <body>
       
      <div id="thebutton">open </div>
       
      <ul id="menu">
      <li>1 </li> <li>2 </li>
      <li>3 </li>
      <li>4 </li>
      <li>5 </li>
      <li id="backbutton">close </li>
      </ul>
       
      </body>
      </html>