JavaScript - Simple Copy Array To Other Array

Submitted by: 
Language: 
Visitors have accessed this post 180 times.

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="<a href="https://sourcecodester.com">Sourcecodster</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodster</a>
  11. </a> </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.  
  54. </div>
  55. </body>
  56. <script src="js/script.js"></script>
  57. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.