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

JavaScript - Simple Copy Array To Other Array

kronicvfx

Side Gig Pro
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
114
Likes
140
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple Copy Array To Other Array using JavaScript. This code will automatically copy an array list to a newly created array when user click the copy button. The code use onclick() to initiate a method that can copy an array list to other array by the using dot notation push(), a special function that force an array element to insert the data to other array element. You can apply this script to your code to make your transaction faster, it 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"

    >

    Sourcecodster</

    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 - Simple Insert Array Data</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <form

    action

    =

    "javascript:void(0);"

    method

    =

    "POST"

    class

    =

    "form-inline"

    onsubmit

    =

    "insert()"

    >
  18. <label

    >

    Full Name:</

    label

    >
  19. <input

    type

    =

    "text"

    id

    =

    "name"

    class

    =

    "form-control"

    /

    >
  20. <button

    type

    =

    "submit"

    class

    =

    "btn btn-primary form-control"

    ><span

    class

    =

    "glyphicon glyphicon-plus"

    ></

    span

    >

    Add</

    button

    >
  21. </

    form

    >
  22. <br

    /

    >
  23. <br

    /

    >
  24. <div

    class

    =

    "col-md-5"

    >
  25. <div

    class

    =

    "alert alert-info"

    >

    Array List 1</

    div

    >
  26. <table

    class

    =

    "table table-bordered"

    >
  27. <thead

    class

    =

    "alert-info"

    >
  28. <tr

    >
  29. <th

    >

    Full Name</

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    id

    =

    "list1"

    >
  33. </

    tbody

    >
  34. </

    table

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-md-2"

    >
  37. <button

    class

    =

    "btn btn-success"

    id

    =

    "copy"

    onclick

    =

    "copyArray(this);"

    disabled

    =

    "disabled"

    >

    Copy</

    button

    >
  38. <br

    /

    ><br

    /

    >
  39. <button

    class

    =

    "btn btn-danger"

    id

    =

    "clear"

    onclick

    =

    "clearAll(this);"

    disabled

    =

    "disabled"

    >

    Clear</

    button

    >
  40. </

    div

    >
  41. <div

    class

    =

    "col-md-5"

    >
  42. <div

    class

    =

    "alert alert-info"

    >

    Array List 2</

    div

    >
  43. <table

    class

    =

    "table table-bordered"

    >
  44. <thead

    class

    =

    "alert-info"

    >
  45. <tr

    >
  46. <th

    >

    Full Name</

    th

    >
  47. </

    tr

    >
  48. </

    thead

    >
  49. <tbody

    id

    =

    "list2"

    >
  50. </

    tbody

    >
  51. </

    table

    >
  52. </

    div

    >

  53. </

    div

    >
  54. </

    body

    >
  55. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  56. </

    html

    >

Creating the Script

This code contains the script of the application. This code will copy an array value 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
  1. var

    list1 =

    document.getElementById

    (

    'list1'

    )

    ;
  2. var

    array1 =

    [

    ]

    ;
  3. var

    array2 =

    [

    ]

    ;


  4. function

    arrayList1(

    )

    {
  5. var

    data =

    ''

    ;
  6. if

    (

    array1.length

    >

    0

    )

    {
  7. for

    (

    i =

    0

    ;

    i <

    array1.length

    ;

    i++

    )

    {
  8. data +=

    '<tr>'

    ;
  9. data +=

    '<td>'

    +

    array1[

    i]

    +

    '</td>'

    ;
  10. data +=

    '</tr>'

    ;
  11. }
  12. }
  13. list1.innerHTML

    =

    data;
  14. }

    ;

  15. function

    arrayList2(

    )

    {
  16. var

    data =

    ''

    ;
  17. if

    (

    array2.length

    >

    0

    )

    {
  18. for

    (

    var

    i =

    0

    ;

    i <

    array2.length

    ;

    i++

    )

    {
  19. data +=

    '<tr>'

    ;
  20. data +=

    '<td>'

    +

    array2[

    i]

    +

    '</td>'

    ;
  21. data +=

    '</tr>'

    ;
  22. }
  23. }

  24. list2.innerHTML

    =

    data;
  25. }

    ;


  26. function

    copyArray(

    btn)

    {
  27. if

    (

    array1.length

    >

    0

    )

    {
  28. for

    (

    var

    i =

    0

    ;

    i <

    array1.length

    ;

    i++

    )

    {
  29. array2.push

    (

    array1[

    i]

    )

    ;
  30. }

  31. arrayList2(

    )

    ;
  32. }

  33. btn.setAttribute

    (

    "disabled"

    ,

    "disabled"

    )

    ;
  34. document.getElementById

    (

    'clear'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  35. }

  36. function

    clearAll(

    btn)

    {
  37. array2=

    [

    ]

    ;
  38. document.getElementById

    (

    'list2'

    )

    .innerHTML

    =

    ""

    ;
  39. document.getElementById

    (

    'copy'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  40. btn.setAttribute

    (

    "disabled"

    ,

    "disabled"

    )

    ;
  41. }

  42. function

    insert(

    )

    {
  43. var

    el =

    document.getElementById

    (

    'name'

    )

    ;
  44. var

    name =

    el.value

    ;
  45. if

    (

    name)

    {
  46. array1.push

    (

    name.trim

    (

    )

    )

    ;
  47. el.value

    =

    ''

    ;
  48. }
  49. arrayList1(

    )

    ;

  50. if

    (

    array2.length

    ==

    0

    )

    {
  51. document.getElementById

    (

    'copy'

    )

    .removeAttribute

    (

    'disabled'

    )

    ;
  52. }
  53. }

    ;

There you have it we successfully created a Simple Copy Array To Other Array 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 the hidden content.
 

452,496

330,760

330,768

Top