How to Dynamically Reorder Element Position in JavaScript

How to Dynamically Reorder Element Position in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Reorder Element Position in JavaScript. This tutorial purpose is to teach how you can reorder an element position. This will tackle all the important functionality that will reorder you current element. 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 system or application if you to have a reorder function. I will give my best to provide you the easiest way of creating this program Reorder Element Position. So let's do the coding.

Before we get started:

Here 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 only display the html list and button to trigger an event. 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. <body onload="displayItem();">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand">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">How to Dynamically Reorder Element Position</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-12">
  18. <button class="btn btn-primary" type="button" onclick="reOrder()">Reorder</button>
  19. <br /><br />
  20. <div id="result"></div>
  21. </div>
  22. </div>
  23. <script src="script.js"></script>
  24. </body>
  25. </html>

Creating JavaScript Function

This is where the main function of the application is. This code reorder your current element when you click the button To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function displayItem(){
  2. var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  3. var html = "";
  4. for(var i=0; i<arr.length; i++){
  5. html += "<div width='200' height='200' style='border:1px solid #000; padding:30px; margin:15px; font-size:50px; float:left;'>"+arr[i]+"</div>";
  6. }
  7. html += "<br style='clear:both;'/>";
  8.  
  9. document.getElementById('result').innerHTML = html;
  10. }
  11.  
  12. function reOrder(){
  13. var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  14. var html = "";
  15. arrayRear(arr).forEach(function(item){
  16. html += "<div width='200' height='200' style='border:1px solid #000; padding:30px; margin:15px; font-size:50px; float:left;'>"+item+"</div>";
  17. })
  18. html += "<br style='clear:both;'/>";
  19.  
  20. document.getElementById('result').innerHTML = html;
  21. }
  22.  
  23. function arrayRear(arr) {
  24. for (var i = 0, rear = [], randomIndex = 0; i < arr.length; i++) {
  25. randomIndex = Math.floor(Math.random() * arr.length);
  26. while (rear.indexOf(arr[randomIndex]) !== -1) {
  27. randomIndex = Math.floor(Math.random() * arr.length);
  28. }
  29. rear.push(arr[randomIndex]);
  30. }
  31.  
  32. return rear;
  33. }

In the code provided we just created only one method called reOrder(), this function will immediately randomize the element position after your trigger the event. The code uses loops to loop through the index position of the elements. Then we use the random() and indexOf() to randomize the exact index position of the elements.

Output:


The How to Dynamically Reorder Element Position in 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 Dynamically Reorder Element Position in 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