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

JavaScript - Display Random Table Content Source Code

Ftbr

Code Ninja
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
126
Likes
79
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Display Random Table Content using JavaScript. This code will display random content in the table when user click the button. The code use JavaScript onclick() to call a specific function that will display the array of object by the use of for() loop, and then rearrange the index position of an array by using array.splice(). 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.

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

    >
  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 - Display Random 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

    =

    "generateContent();"

    >

    Random Content</

    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

    >

    Address</

    th

    >
  26. </

    tr

    >
  27. </

    thead

    >
  28. <tbody

    id

    =

    "result"

    ></

    tbody

    >
  29. </

    table

    >
  30. </

    div

    >
  31. </

    div

    >
  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 display random content in the table when the user click the button. 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

    generateContent(

    )

    {
  2. var

    member =

    [
  3. {

    firstname:

    "John"

    ,

    lastname:

    "Smith"

    ,

    address:

    "New York"

    }

    ,
  4. {

    firstname:

    "Claire"

    ,

    lastname:

    "Temple"

    ,

    address:

    "Racoon City"

    }

    ,
  5. {

    firstname:

    "Tony"

    ,

    lastname:

    "Stank"

    ,

    address:

    "Japan"

    }

    ,
  6. {

    firstname:

    "Izuku"

    ,

    lastname:

    "Midoriya"

    ,

    address:

    "Academia"

    }

    ,
  7. {

    firstname:

    "Tom"

    ,

    lastname:

    "Jerry"

    ,

    address:

    "Paris"

    }
  8. ]

    ;

  9. var

    html =

    ""

    ;

  10. var

    array=

    randomContent(

    member)

    ;
  11. for

    (

    var

    i =

    0

    ;

    i <

    array.length

    ;

    i++

    )

    {

  12. if

    (

    i<

    3

    )

    {
  13. html+=

    "<tr>"

    ;
  14. html+=

    "<td>"

    +

    array[

    i]

    .firstname

    +

    "</td>"

    ;
  15. html+=

    "<td>"

    +

    array[

    i]

    .lastname

    +

    "</td>"

    ;
  16. html+=

    "<td>"

    +

    array[

    i]

    .address

    +

    "</td>"

    ;

  17. html+=

    "</tr>"

    ;
  18. }
  19. }

  20. document.getElementById

    (

    "result"

    )

    .innerHTML

    =

    html;
  21. }

  22. function

    randomContent(

    array)

    {
  23. var

    temp =

    [

    ]

    ;
  24. var

    len=

    array.length

    ;
  25. while(

    len)

    {
  26. temp.push

    (

    array.splice

    (

    Math

    .floor

    (

    Math

    .random

    (

    )

    *

    array.length

    )

    ,

    1

    )

    [

    0

    ]

    )

    ;
  27. len--;
  28. }
  29. return

    temp;
  30. }

There you have it we successfully created a Display Random Table Content 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!


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

452,292

323,341

323,350

Top