JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old July 27th, 2004, 04:23 PM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Placing a value into a text box

Hi,
I'm trying to get some different values into different text boxes. Here is some of the code:

Code:
hisCookie = document.cookie.split("::"); // Puts each product into the thisCookie array
                        
                        // Variables which are used in the loop to create the shopping basket table
                        var table1 = '<table width="516"  border="1" cellspacing="0" cellpadding="0"><tr><td width="335">'
                        var table2 = '</td><td width="95">'
                        var table3 = '<form name="add" id="form1" method="post" action=""><input name="amount" type="text" id="amount" value="1" size="2" maxlength="2" /></form>'
                        var table4 = '</td><td width="78">'
                        var table5 = '</td></tr></table>'
                        var amounts= new Array();
                        
                        var total = 0.00;
                        document.write(table1+"Item"+table2+" "+table4+"Price"+table5);

                        // 'for loop' splits each item in thisCookie array at the '=' sign, this takes out the cookie name from the string (i.e 'cost')
                        for(j=0; j<thisCookie.length-1; j++){
                                thisCookies = thisCookie[j].split("=");
                                
                          // 'for loop' splits each item in 'thisCookies' array at each '/' and then places each part in a table        
                          for (i=1; i<thisCookies.length; i++){
                             pieces = thisCookies[i].split("/");
                                 document.write(table1+pieces[1]+table2+table3+table4+"£"+pieces[0]+table5);
                             total += parseFloat(pieces[0]);
                          } 
                          amounts[j] = pieces[2];
                         
                          var something = amounts[j];
                          document.add.amount.value = something;
                          }


The part highlighted in Bold is the problem, before l added that the rest worked fine. The code splits up a cookie into the relevant parts and then creates a table for each item in the cookie. Each item has 3 properties, the third property is an amount. I have created a text box that holds this amount. What l want is for the amounts to be inputted into their matching text box. Above the amounts are put into an array 'amounts' which works correctly. Then l want to take each of these in turn and put them into each text box, which is why this is left inside the loop.

But l keep getting an error message saying that it is not an object. So l think that means that the table has not yet been created when l ask for the text box to equal an amount. So how can l achieve this without taking the 'amount' out of the loop??


Reply With Quote
  #2  
Old July 27th, 2004, 09:55 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Probably has more to do with overlapping names. All printed forms have the name "add," and when you try to set the element by invoking document.add.amount.value, the browser's probably freaking out because it doesn't know which form to use. Could also be a product of your writing the form in js, as you've suggested.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old July 28th, 2004, 04:39 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks l see what you mean. In this case there are 3 different forms created but they all have the same names and input names. Hmm, do you have any ideas on how to get around this. I need the forms to be created like this but how would l put the names in a loop so a number attached to them could increment everytime a form is created?? (e.g form1, form2, form3) Then l could add the amounts to each form in a loop.

Reply With Quote
  #4  
Old July 28th, 2004, 07:04 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Might be easier to give the forms random names and invoke them as document.forms[0].fieldname.value instead of using the name. To do this, you'd just need to maintain a count during your loop, which count you could also use to refer to the appropriate form.

Reply With Quote
  #5  
Old July 28th, 2004, 08:53 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Got some help from another forum, this is what the solution is for anyone else who has this problem:

Code:
<SCRIPT LANGUAGE="javascript" type="text/JavaScript"> // This script splits the cookie into each item entry and then splits each entry into it's elements and puts these into a table

		 
		 	thisCookie = document.cookie.split("::"); // Puts each product into the thisCookie array
		 	
			// Variables which are used in the loop to create the shopping basket table
			var table1 = '<table width="516"  border="1" cellspacing="0" cellpadding="0"><tr><td width="335">'
			var table2 = '</td><td width="95">'
			var table3 = '<input name="amount" type="text" id="amount" value="1" size="2" maxlength="2" /></form>'
			var table4 = '</td><td width="78">'
			var table5 = '</td></tr>'
			var table6 = '<tr><td width="335">'
			var amounts= new Array();
			
			document.write('<form name="add" id="form1" method="post" action="">');
			var total = 0.00;
			document.write(table1+"Item"+table2+" "+table4+"Price"+table5);
			document.write('<table width="516"  border="1" cellspacing="0" cellpadding="0">');
			
			// 'for loop' splits each item in thisCookie array at the '=' sign, this takes out the cookie name from the string (i.e 'cost')
			for(j=0; j<thisCookie.length-1; j++){
				thisCookies = thisCookie[j].split("=");
				
			  // 'for loop' splits each item in 'thisCookies' array at each '/' and then places each part in a table	
			  for (i=1; i<thisCookies.length; i++){
			    pieces = thisCookies[i].split("/");
				document.write('<tr><td width="335">');
				document.write(pieces[1]);
				document.write('</td><td width="95">');
				document.write('<input name="amount'+j+'" type="text" id="amount_'+j+'" value="'+pieces[2]+'" size="2" maxlength="2" />');
				document.write('</td><td width="78">');
				document.write("£");
				document.write(pieces[0]);
				document.write('</td></tr>');

			    total += parseFloat(pieces[0]);
			  } 
			  }
			  document.write('</table>');
			  document.write('</form>');


Thanks for your time though.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Placing a value into a text box


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway