• We just launched and are currently in beta. Join us as we build and grow the community.

How to Randomize HTML Table Content in JavaScript

jaydenthemasterof

Platform API Engineer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
32
Likes
94
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
How to Randomize HTML Table Content in JavaScript

Introduction

In this tutorial we will create a How to Randomize HTML Table Content in JavaScript. This tutorial purpose is to teach you how to randomize the table content. This will tackle all the basic function that will display a random table content. 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 that use table to display some data. I will give my best to provide you the easiest way of creating this program Randomize table content. 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 the buttons and table content. 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

    =

    "displayTable();"

    >
  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"

    >

    How to Randomize HTML Table Content</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    onclick

    =

    "displayTable();"

    >

    Randomize</

    button

    >
  19. <br

    /

    ><br

    /

    >
  20. <table

    class

    =

    "table table-bordered"

    >
  21. <thead

    class

    =

    "alert-info"

    >
  22. <tr

    >
  23. <th

    >

    Firstname</

    th

    >
  24. <th

    >

    Lastname</

    th

    >
  25. <th

    >

    Age</

    th

    >
  26. </

    tr

    >
  27. </

    thead

    >
  28. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  29. </

    table

    >
  30. </

    div

    >
  31. </

    div

    >
  32. <script

    src

    =

    "script.js"

    ></

    script

    >
  33. </

    body

    >
  34. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically randomize the table content when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    shuffleArray(

    array)

    {
  2. var

    temp =

    [

    ]

    ;
  3. var

    len=

    array.length

    ;
  4. while(

    len)

    {
  5. temp.push

    (

    array.splice

    (

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    array.length

    )

    ,

    1

    )

    [

    0

    ]

    )

    ;
  6. len--;
  7. }
  8. return

    temp;
  9. }

  10. function

    displayTable(

    )

    {
  11. var

    firstname =

    [

    "John"

    ,

    "Steve"

    ,

    "Luke"

    ,

    "Bruce"

    ]

    ;
  12. var

    lastname =

    [

    "Smith"

    ,

    "Wonder"

    ,

    "Hulgan"

    ,

    " Banner"

    ]

    ;
  13. var

    age =

    [

    "38"

    ,

    "32"

    ,

    "31"

    ,

    " 34"

    ]

    ;
  14. var

    newArray1=

    shuffleArray(

    firstname)

    ;
  15. var

    newArray2=

    shuffleArray(

    lastname)

    ;
  16. var

    newArray3=

    shuffleArray(

    age)

    ;
  17. var

    html=

    ""

    ;

  18. for

    (

    var

    i=

    0

    ;

    i<

    newArray1.length

    ;

    i++

    )

    {
  19. html+=

    "<tr><td>"

    +

    newArray1[

    i]

    +

    "</td><td>"

    +

    newArray2[

    i]

    +

    "</td><td>"

    +

    newArray3[

    i]

    +

    "</td></tr>"

    ;
  20. }

  21. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  22. }

In the code above we will create a method called shuffleArray(), this function will randomize the data in array when called. The function is consist of loops that will append the data and splice it to get a random result.

Output:

How%20to%20Randomize%20HTML%20Table%20Content%20in%20JavaScript%201.png

The How to Randomize HTML Table Content 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 Randomize HTML Table Content 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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,193

322,229

322,238

Top