How to insert data into array using javaScript

<!DOCTYPE html>
<html>
<body>

<p>Click the button to add a new element to the array.</p>
<input type="text" id="name">
<button onclick="myFunction()">insert</button>

<p id="demo"></p>

<script>

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
var data1  = document.getElementById("name").value;
    fruits.push(data1);
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

Previous
Next Post »

Disqus Shortname

Comments system