Iterate trough array - 1,2,3,1,2,3...etc


/ Published in: jQuery
Save to your folder(s)

This is perfect for "cycling" items...


Copy this code and paste it in your HTML
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JS literations</title>
  5. <meta charset="UTF-8">
  6.  
  7. <script language="Javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  8.  
  9. <script type="text/javascript">
  10.  
  11. $(function(){
  12.  
  13. var volumes = ["10", "20", "30", "40", "50", "55", "60", "80", "100"];
  14. var currentVolume = -1;
  15.  
  16. $("#buttonClick").click(function(){
  17. currentVolume = ( ++currentVolume % volumes.length );
  18. $("#log").html("volume: " + volumes[currentVolume] + " %");
  19. return false;
  20. });
  21. $("#buttonClick").click();
  22. });
  23.  
  24. </script>
  25.  
  26. </head>
  27. <body>
  28. <a href="#" id="buttonClick">change volume<a>
  29. <p id="log">volume</p>
  30. </body>
  31. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.