Skip to main content

JavaScript - Simple Copy Array To Other Array


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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascript - Simple Insert Array Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <form action="javascript:void(0);" method="POST" class="form-inline" onsubmit="insert()">
  17. <label>Full Name:</label>
  18. <input type="text" id="name" class="form-control"/>
  19. <button type="submit" class="btn btn-primary form-control"><span class="glyphicon glyphicon-plus"></span> Add</button>
  20. </form>
  21. <br />
  22. <br />
  23. <div class="col-md-5">
  24. <div class="alert alert-info">Array List 1</div>
  25. <table class="table table-bordered">
  26. <thead class="alert-info">
  27. <tr>
  28. <th>Full Name</th>
  29. </tr>
  30. </thead>
  31. <tbody id="list1">
  32. </tbody>
  33. </table>
  34. </div>
  35. <div class="col-md-2">
  36. <button class="btn btn-success" id="copy" onclick="copyArray(this);" disabled="disabled">Copy</button>
  37. <br /><br />
  38. <button class="btn btn-danger" id="clear" onclick="clearAll(this);" disabled="disabled">Clear</button>
  39. </div>
  40. <div class="col-md-5">
  41. <div class="alert alert-info">Array List 2</div>
  42. <table class="table table-bordered">
  43. <thead class="alert-info">
  44. <tr>
  45. <th>Full Name</th>
  46. </tr>
  47. </thead>
  48. <tbody id="list2">
  49. </tbody>
  50. </table>
  51. </div>
  52.  
  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.  
  5.  
  6. function arrayList1() {
  7. var data = '';
  8. if (array1.length > 0) {
  9. for (i = 0; i < array1.length; i++) {
  10. data += '<tr>';
  11. data += '<td>' + array1[i] + '</td>';
  12. data += '</tr>';
  13. }
  14. }
  15. list1.innerHTML = data;
  16. };
  17.  
  18. function arrayList2() {
  19. var data = '';
  20. if (array2.length > 0) {
  21. for (var i = 0; i < array2.length; i++) {
  22. data += '<tr>';
  23. data += '<td>' + array2[i] + '</td>';
  24. data += '</tr>';
  25. }
  26. }
  27.  
  28. list2.innerHTML = data;
  29. };
  30.  
  31.  
  32. function copyArray(btn){
  33. if(array1.length > 0){
  34. for (var i = 0; i < array1.length; i++) {
  35. array2.push(array1[i]);
  36. }
  37.  
  38. arrayList2();
  39. }
  40.  
  41. btn.setAttribute("disabled", "disabled");
  42. document.getElementById('clear').removeAttribute('disabled');
  43. }
  44.  
  45. function clearAll(btn){
  46. array2=[];
  47. document.getElementById('list2').innerHTML = "";
  48. document.getElementById('copy').removeAttribute('disabled');
  49. btn.setAttribute("disabled", "disabled");
  50. }
  51.  
  52. function insert() {
  53. var el = document.getElementById('name');
  54. var name = el.value;
  55. if (name) {
  56. array1.push(name.trim());
  57. el.value = '';
  58. }
  59. arrayList1();
  60.  
  61. if (array2.length == 0) {
  62. document.getElementById('copy').removeAttribute('disabled');
  63. }
  64. };

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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.