JavaScript array replace element

In this Tutorial we want to describe you a code that makes you to understand array reset.

JavaScript array replace element


In this Tutorial we want to describe you a code that makes you to understand  array reset. The code create you a HTML Page specifying name, text field and two button i.e. Insert and Reset. On click a insert button push  ( ) and show ( ) function is invoked. The push function include a conditional loop if  i.e., in case the value is null, alert box show you a message 'Name is empty', Otherwise the value entered by you in a text field is assigned to array. The count variable count the number of element inserted. The show function include an array .length( ) return you the length of an element in array. The for loop run and execute the script till variable i is less than count. The string concatenated with array and assigned to a variable string. If the length of array is greater than zero, the document.getElementById return you the element in the string on the basis of respective Id.

On click a Reset button, a reset function ( ) is invoked, this function erase or reset the value inserted by you.

JavaScript array
<title>Reset Array</title>
var array = new Array();
var count = 0;
var string;
function push(val){
alert("Name is emplty")
else {

function show() {
string="<hr><b>Array Length :</b><b>"+array.length+"</b><br>";
for(i = 0; i < count; i++) {
string =string+array[i]+"<br>";

if(array.length > 0)
document.getElementById('myDiv').innerHTML = string;

function resetFunction(){
count = 0;
document.getElementById('myDiv').innerHTML = string;
<h2>Reset Array</h2>
<form name="form1">
<table width="407">
<td width="154" align="right"><b>Name</b></td>
<td width="9"><b>&nbsp;:</b></td>
<td width="224">
<input type="text" name="name"/></td>
<td width="154" align="right">&nbsp;</td>
<td width="9">&nbsp;</td>
<td width="224">&nbsp;</td>
<td width="154" align="right">&nbsp;</td>
<td width="9">&nbsp;</td>
<td width="224">
<input type="button" Value=" Insert " 
<input type="button" Value=" Reset " 
<div id="myDiv"></div>


Download code
Share on Google+Share on Google+

JavaScript array replace element

Posted on: November 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JavaScript array replace element  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image