954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with array of objects in javascript

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"/>

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

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

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

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

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


</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>
STUDENT#101
Newbie Poster
16 posts since Aug 2008
Reputation Points: 8
Solved Threads: 1
 

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

[TEX]document.getElementById();[/TEX]
[TEX]document.createElement();[/TEX]
[TEX]element.appendChild();[/TEX]

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

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You