JavaScript - Display Random Table Content Source Code

In this tutorial we will create a Display Random Table Content using JavaScript. This code will display random content in the table when user click the button. The code use JavaScript onclick() to call a specific function that will display the array of object by the use of for() loop, and then rearrange the index position of an array by using array.splice(). You can apply this script to your system that use image gallery, this is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Display Random Table Content</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <button type="button" class="btn btn-primary" onclick="generateContent();">Random Content</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Address</th>
  25. </tr>
  26. </thead>
  27. <tbody id="result"></tbody>
  28. </table>
  29. </div>
  30. </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will display random content in the table when the user click the button. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function generateContent(){
  2. var member = [
  3. {firstname: "John", lastname: "Smith", address: "New York"},
  4. {firstname: "Claire", lastname: "Temple", address: "Racoon City"},
  5. {firstname: "Tony", lastname: "Stank", address: "Japan"},
  6. {firstname: "Izuku", lastname: "Midoriya", address: "Academia"},
  7. {firstname: "Tom", lastname: "Jerry", address: "Paris"}
  8. ];
  9.  
  10. var html = "";
  11.  
  12. var array=randomContent(member);
  13. for (var i = 0; i <array.length; i++) {
  14.  
  15. if(i<3){
  16. html+="<tr>";
  17. html+="<td>"+array[i].firstname+"</td>";
  18. html+="<td>"+array[i].lastname+"</td>";
  19. html+="<td>"+array[i].address+"</td>";
  20.  
  21. html+="</tr>";
  22. }
  23. }
  24.  
  25. document.getElementById("result").innerHTML = html;
  26. }
  27.  
  28. function randomContent(array) {
  29. var temp = [];
  30. var len=array.length;
  31. while(len){
  32. temp.push(array.splice(Math.floor(Math.random()*array.length),1)[0]);
  33. len--;
  34. }
  35. return temp;
  36. }
There you have it we successfully created a Display Random Table Content using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment