Display values of Array with a Loop


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



Copy this code and paste it in your HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>Sorting</title>
  5. <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js?ver=3.2.1'></script>
  6.  
  7. <script type="text/javascript">
  8. $('document').ready(function () {
  9.  
  10. var arr = [52, 97, 3, 62, 10, 63, 64, 1, 9, 3, 4];
  11.  
  12. for (i = 0; i < arr.length; i++) {
  13. $('p#test' + i).html(arr[i]);
  14. }
  15.  
  16. });
  17.  
  18. </script>
  19. </head>
  20. <body>
  21. <h1>This is a sorting example</h1>
  22. <p id="test0">test</p>
  23. <p id="test1">test</p>
  24. <p id="test2">test</p>
  25. <p id="test3">test</p>
  26. <p id="test4">test</p>
  27. <p id="test5">test</p>
  28. <p id="test6">test</p>
  29. <p id="test7">test</p>
  30. <p id="test8">test</p>
  31. <p id="test9">test</p>
  32. <p id="test10">test</p>
  33. <div><input type="button" value="sort" id="sortButton" /></div>
  34. </body>
  35. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.