JavaScript - Insert New Array Value
Submitted by razormist on Wednesday, January 15, 2020 - 21:45.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form action="javascript:void(0);" method="POST" onsubmit="insert()">
- <div class="form-group">
- <input type="text" id="full_name" class="form-control"/>
- </div>
- </form>
- </div>
- <div class="col-md-8">
- </div>
- </div>
- </body>
- </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- var members = ["John Smith"];
- displayArray();
- function displayArray(){
- document.getElementById('array').innerHTML=JSON.stringify(members, null, 2);
- }
- function insert() {
- var el = document.getElementById('full_name');
- var full_name = el.value;
- if (full_name == "") {
- alert("Please enter something first!");
- }else{
- members.push(full_name.trim());
- el.value = '';
- displayArray();
- }
- };
Add new comment
- 69 views