JavaScript - Random Order Content Using jQuery

Submitted by: 
Visitors have accessed this post 143 times.

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

And this is the link for the jquery that i used in this tutorial

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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Random Order Content Using jQuery</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="content alert alert-info">
  19. <h3>PHP</h3>
  20. <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>
  21. </div>
  22. <div class="content alert alert-danger">
  23. <h3>JavaScipt</h3>
  24. <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>
  25. </div>
  26. <div class="content alert alert-success">
  27. <h3>Python</h3>
  28. <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>
  29. </div>
  30. </div>
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.