title: Previous Next in Javascript
body: <p>I am trying to implement previous next functionality in Javascript but it only gives me the next element.</p>
  
  <p>I have used currentItemIndex as the reverence to start for the buttons. I am starting from 5th Item and on click of next button I am trying to get 6th, 7th 8th and so on. and vice versa for previous button.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Previous Next Functionality&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
  
      var myItemObjectArray = new Array();
      var currentItemIndex = 5;
  
      alert("CURRENT ITEM INDEX " + currentItemIndex);
      for(i=0;i&lt;10;i++)
          {
              var ItemCatalog = new Object();
  
              ItemCatalog.itemId = i;
              ItemCatalog.itemName = "a"+i;
  
              myItemObjectArray.push(ItemCatalog);
              /*alert("OBJECT ADDED " + myItemObjectArray.length);*/
          }
  
  
      function getPrevious(currentItemIndex, myItemObjectArray)
          {
              var localCurrentItemIndex = currentItemIndex-1;
              alert("PREVIOUS OBJECT" + myItemObjectArray[localCurrentItemIndex].itemId + " " + myItemObjectArray[localCurrentItemIndex].itemName);
  
              // Modify Current Item Index
              currentItemIndex--;
              alert(currentItemIndex);
          }   
  
      function getNext(currentItemIndex, myItemObjectArray)
          {
              var localCurrentItemIndex = currentItemIndex+1; 
              alert("NEXT OBJECT" + myItemObjectArray[localCurrentItemIndex].itemId + " " + myItemObjectArray[localCurrentItemIndex].itemName);
              // Modify Current Item Index
              currentItemIndex++;
              alert(currentItemIndex);
          }   
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;button id="previous" onclick="getPrevious(currentItemIndex, myItemObjectArray)"&gt;Previous&lt;/button&gt;
      &lt;button id="next" onclick="getNext(currentItemIndex, myItemObjectArray)"&gt;Next&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The same element keeps getting repeated.</p>
  
  <p>Ankit</p>