How to Display Images in a Random Order using JavaScript

How to Display Images in a Random Order using JavaScript

Introduction

In this tutorial we will create a How to Display Images in a Random Order using JavaScript. This tutorial purpose is to teach how to randomize the displayed images. This will cover all the important functions that provide the image random display. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any kind of system that needed to showcase some image gallery. I will give my best to provide you the easiest way of creating this program Display Image in Random Order. So let's do the coding.

Before we get started:

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

Creating The Interface

This is where we will create a simple interface for our application. This code will display image holder and button. To create this simply copy and write it into your text editor, then save it 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">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">How to Display Images in a Random Order using JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div id="result" style="padding:30px;"></div>
  17. <button class="btn btn-primary" type="button" onclick="shuffle()">Random</button>
  18. </div>
  19. <script src="script.js"></script>
  20. </body>
  21. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will let you dynamically randomize the image order. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. shuffle();
  2.  
  3.  
  4.  
  5. function shuffle(){
  6. var arr = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", "image6.jpg"];
  7. var html = "";
  8. shuffleArray(arr).forEach(function(item){
  9. html += "<img src='images/"+item+"' width='170' height='150' style='margin:10px; float:left;'>";
  10. })
  11. html += "<br style='clear:both;'/>";
  12.  
  13. document.getElementById('result').innerHTML = html;
  14. }
  15.  
  16. function shuffleArray(arr) {
  17. for (var i = 0, shuffled = [], randomIndex = 0; i < arr.length; i++) {
  18. randomIndex = Math.floor(Math.random() * arr.length);
  19. while (shuffled.indexOf(arr[randomIndex]) !== -1) {
  20. randomIndex = Math.floor(Math.random() * arr.length);
  21. }
  22. shuffled.push(arr[randomIndex]);
  23. }
  24.  
  25. return shuffled;
  26. }

In the first line of code you notice we already call the method for displaying random images called shuffle(). In the first method we create we only set some variables that will hold the image as an array and display html content. We now called the shuffleArray() it is the method that we coded. This method contains the logic of randomizing the images. For starter it contains a different loops and use a random function called Math.floor(), that will randomize a certain value or index.

Output:

The How to Display Images in a Random Order using JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Display Images in a Random Order 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment