JavaScript - Get The First Value In Array
Submitted by razormist on Thursday, January 23, 2020 - 09:20.
In this tutorial we will create a JavaScript - Get The First Value In Array using JavaScript. This code will retrieve the first data of an array when the user click the button. The code use onclick() to call a specific a method that will slice the array data by adding a parameter [0] in the array object as an index position. This program is a user-friendly. Feel free to use it in your system.
We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.
There you have it we successfully created a JavaScript - Get The First Value In 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!
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-5">
- </div>
- <div class="col-md-6">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- </tr>
- </thead>
- <tbody id="result">
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will get the first array key 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 arrays = ["PHP", "JavaScript", "Python", "C#", "C++"];
- displayData(arrays);
- function displayData(array){
- var html="";
- for(var i=0; i<array.length; i++){
- html +="<tr>";
- html +="<td>"+array[i]+"</td>";
- html +="</tr>";
- }
- document.getElementById('result').innerHTML=html;
- }
- document.getElementById("get").addEventListener("click", getFirstData.bind(this, arrays));
- function getFirstData(array){
- var data = array[0];
- document.getElementById('display').innerHTML="<center><label>The first data of an array is: <span class='text-primary'>"+data+"</span></label></center>";
- }
Add new comment
- 250 views