JavaScript - Reorder Element Position
Submitted by razormist on Monday, December 30, 2019 - 13:18.
In this tutorial we will create a Reorder Element Position using JavaScript. This code will reorder the element position to a new placement when user click the button. The process use JavaScript onclick() function to call a specific method that use for() loop to compress an array and reorder the index in order to display it different. 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.
There you have it we successfully created a Reorder Element Position 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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-5">
- <br />
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will reorder the element position when the button is clicked. 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- displayItem();
- function displayItem(){
- var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- var html = "";
- for(var i=0; i<arr.length; i++){
- html += "<div width='200' height='200' style='border:1px solid #000; padding:10px; margin:10px; font-size:50px; float:left;'>"+arr[i]+"</div>";
- }
- html += "<br style='clear:both;'/>";
- document.getElementById('result').innerHTML = html;
- }
- function arrOrder(){
- var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- var html = "";
- arrayRear(arr).forEach(function(item){
- html += "<div width='200' height='200' style='border:1px solid #000; padding:10px; margin:10px; font-size:50px; float:left;'>"+item+"</div>";
- })
- html += "<br style='clear:both;'/>";
- document.getElementById('result').innerHTML = html;
- }
- function arrayRear(arr) {
- for (var i = 0, rear = [], randomIndex = 0; i < arr.length; i++) {
- randomIndex = Math.floor(Math.random() * arr.length);
- while (rear.indexOf(arr[randomIndex]) !== -1) {
- randomIndex = Math.floor(Math.random() * arr.length);
- }
- rear.push(arr[randomIndex]);
- }
- return rear;
- }
Add new comment
- 197 views