i am just beginning javascript so i decided to make a program that accepts values from the user on a button click and stores them in an array of object and on click of another button show writes them to the innerHTML of a paragraph
<!DOCTYPE HTML>
<html>
<head>
<script>
var records = new Array();
var counter = 0;
var t = document.getElementById("show");
function record(name,age,cla)
{
this.name=name;
this.age=age;
this.cla=cla;
}
function add()
{
var r1 = new record(frm1.name.value,frm1.age.value,frm1.clas.value);
alert(r1.name);
records.push(r1);
alert(records[0].name);
}
function show()
{alert(records.length);
for(var i=counter;i<records.length;i++,counter++)
{
t.innerHTML+= records[i].name+" "+records[i].age+" "+records[i].cla+"<br>";
}
}
function clear()
{
t.innerHTML="";
counter=0;
}
</script>
</head>
<body>
<form id = "frm1" >
name : <input type = text id = "name">
age : <input type = text id = "age">
class : <input type = text id = "clas">
<button onclick= "add()">add</button>
<button onclick = "show()">show</button>
<button onclick = "clear()">clear</button>
</form><br>
<p id = "show"></p>
</body>
</html>
so after debugging it i found out that the element is added properly but whenever i call the show method the records.length shows 0 .. why is the array resetting itself ?