|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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?? ![]() |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
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.
|
|
#5
|
|||
|
|||
|
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. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Placing a value into a text box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|