JavaScript - Insert New Array Value

In this tutorial we will create a Insert New Array Value using JavaScript. This code will insert a new array value when the user click the add button. The code use onclick() to call specific method that can insert a new array value in an array objects. This is free program, feel free to modify it and use it to your working projects. 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 - Insert New Array Value</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <form action="javascript:void(0);" method="POST" onsubmit="insert()">
  18.                                 <div class="form-group">
  19.                                         <label>Full Name:</label>
  20.                                         <input type="text" id="full_name" class="form-control"/>
  21.                                 </div>
  22.                                 <center><button type="submit" class="btn btn-primary">Add</button></center>
  23.                         </form>
  24.                 </div>
  25.                 <div class="col-md-8">
  26.                         <pre id="array"></pre>
  27.                 </div>
  28.         </div>
  29. </body>
  30. <script src="js/script.js"></script>
  31. </html>

Creating the Script

This code contains the script of the application. This code will insert a new 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 members = ["John Smith"];
  2.  
  3.  
  4. displayArray();
  5.  
  6.  
  7. function displayArray(){
  8.         document.getElementById('array').innerHTML=JSON.stringify(members, null, 2);
  9. }
  10.  
  11. function insert() {
  12.     var el = document.getElementById('full_name');
  13.         var full_name = el.value;
  14.         if (full_name == "") {
  15.                 alert("Please enter something first!");
  16.         }else{
  17.                 members.push(full_name.trim());
  18.                 el.value = '';
  19.                 displayArray();
  20.         }
  21. };
There you have it we successfully created a Insert New Array Value 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