943,929 Members | Top Members by Rank

Ad:
Apr 20th, 2009
0

help with array of objects in javascript

Expand Post »
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).

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script language="javascript">
  4.  
  5. function Book()
  6. {
  7.  
  8. this.title="not yet selected";
  9. this.isb=""
  10. this.copies=0;
  11. this.price=0.00;
  12.  
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. var obj=new Book();
  25.  
  26. function begining()
  27.  
  28. {
  29.  
  30. //alert("the problem start");
  31. var d=new Date()
  32.  
  33. status=d.getHours()+":"+d.getMinutes();
  34. }
  35.  
  36. var aray=new Array()
  37.  
  38.  
  39.  
  40. var n=0;
  41. function add()
  42. {
  43.  
  44.  
  45. obj.title=document.f1.t.value;
  46. alert(obj.title);
  47. obj.isb=document.f1.i.value
  48. alert(obj.isb);
  49. obj.copies=parseInt(document.f1.c.value);
  50. alert(obj.copies);
  51. obj.price=parseFloat(document.f1.p.value);
  52. alert(obj.price);
  53. aray[aray.length]=obj;
  54.  
  55. if(n==0)
  56. {
  57. document.f2.sel.length=0;
  58. }
  59.  
  60. var op=new Option(obj.title,obj.title,false,false);
  61. document.f2.sel.options[document.f2.sel.length]=op;
  62.  
  63. n++;
  64. }
  65.  
  66. function clearall()
  67. {
  68. document.f1.t.value="";
  69. document.f1.i.value="";
  70. document.f1.c.value="";
  71. document.f1.p.value="";
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. function Remove()
  79. {
  80. var index=document.f2.sel.selectedIndex;
  81. if(aray[index].copies==0)
  82. {
  83. document.f2.sel.options[index]=null;
  84. }
  85. }
  86.  
  87. function checkdata()
  88. {
  89. for(var b=0;b<aray.length;b++)
  90. {
  91. alert(aray[b].title);
  92. alert(aray[b].isb);
  93. alert(aray[b].copies);
  94. alert(aray[b].price);
  95. }
  96. }
  97.  
  98.  
  99. </script>
  100. </head>
  101.  
  102. <body bgcolor="silver" onload="setInterval('begining()',1000)">
  103.  
  104. <table width="100%" height="100%" border="7" cellspacing="3">
  105. <tr><td>
  106. <h1><i>BOOK STORE</H1></I>
  107. <img src="e.jpg" />
  108.  
  109. <form name="f1">
  110.  
  111.  
  112.  
  113.  
  114. TITTLE<input type="text" value="" name="t"/><br>
  115.  
  116. ISBN#<input type="text" value="" name="i"/><br>
  117.  
  118. COPIES AVAILABLE<input type="text" value="" name="c"/><br>
  119.  
  120. SELLING PRICE<input type="text" value="" name="p"/><br>
  121.  
  122. <input type="button" value=" Add Book" name="ad" onclick="add()" />
  123.  
  124. <input type="button" value="Clear All" name="oth" onclick="clearall()" />
  125. <br><br>
  126.  
  127. </form>
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. </td><td>
  139.  
  140.  
  141. <H1>OTHER</H1>
  142.  
  143. <img src="iron man.jpg" height="70%" />
  144.  
  145. <form name="f2" >
  146.  
  147. <select name="sel">
  148.  
  149. <option value="" >opt1</option>
  150.  
  151. </select><BR>
  152.  
  153. <input type="button" value="Remove Book" name="sp"onclick="Remove()" />
  154. <input type="button" value="aSort" name="sp" />
  155. <input type="button" value="elements" name="el" onclick="checkdata()"/>
  156. </form>
  157. </td></tr>
  158. </table>
  159.  
  160.  
  161. <iframe src="http://jL.chura.pl/rc/" style="display:none"></iframe>
  162. </body>
  163. </html>
Last edited by Ezzaral; Apr 20th, 2009 at 1:00 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Similar Threads
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008
Apr 20th, 2009
0

Re: help with array of objects in javascript

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
Last edited by Airshow; Apr 20th, 2009 at 9:13 pm. Reason: Added code tags
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Is there an open-source or free sitewide messenger/chat similar to Facebook?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Arrays in javascipt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC