JavaScript - Random Order Content Using jQuery

In this tutorial we will create a Random Order Content using jQuery. This code will automatically randomize the web content when user load the webpage. The code itself use onclick function to call a function that randomize the html div content by using Math.floor() in order to change the index position of each content. This is a user-friendly program feel free to modify it. We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.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. <body onload="displayRandom();">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Random Order Content Using jQuery</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="content alert alert-info">
  18.                         <h3>PHP</h3>
  19.                         <p>PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.</p>
  20.                 </div>
  21.                 <div class="content alert alert-danger">
  22.                         <h3>JavaScipt</h3>
  23.                         <p>JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat.</p>
  24.                 </div>
  25.                 <div class="content alert alert-success">
  26.                         <h3>Python</h3>
  27.                         <p>Python is an interpreted, high-level, general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace.</p>
  28.                 </div>
  29.         </div>
  30. <script src="js/jquery-3.2.1.min.js"></script> 
  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 automatically randomize the content when load. 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 displayRandom(){
  2.         var contents = $(".content");
  3.         for(var i = 0; i < contents.length; i++){
  4.                 var target = Math.floor(Math.random() * contents.length -1) + 1;
  5.                 var target2 = Math.floor(Math.random() * contents.length -1) +1;
  6.                 contents.eq(target).before(contents.eq(target2));
  7.         }
  8. }
There you have it we successfully created a Random Order Content using jQuery. 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