spires Posted June 23, 2009 Share Posted June 23, 2009 Hi Guys I need help trying to create and display an array. I have a dropdown list, when an option is selected, this needs to be stored in an array. Then each array needs to be displayed in a text area. You can select multiple options, giving you multiple values in the text area. Please see: http://www.businessmobiles.com/comcalc/test4.php Any help would be great. Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> <script language="javascript"> function notes() { var nList = document.getElementById("notesDropdown").value; var notesArray = new Array(); notesArray[] = nList; for (x = 0; x < numListedInArray; x++){ document.getElementById("finalNotes").value=notesArray[x]+','; } } </script> </head> <body> <form name="finalNotesList" action="test4.php" method="post"> <select name="notesDropdown" size="0" onchange="notes()" id="notesDropdown" style="width:220px; font-size:10px;" > <option value=""> </option> <option value="Number port on Line 1">Number port on Line 1</option> <option value="Add International Roaming to Line">Add International Roaming to Line</option> <option value="Add International Call">Add International Call</option> <option value="Upgrade on Line">Upgrade on Line</option> </select> <br><br> <textarea name="finalNotes" rows="3" cols="75" id="finalNotes" value=""></textarea> <br><br> <input type="submit" name="finalNotesList" value="Submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
spires Posted June 23, 2009 Author Share Posted June 23, 2009 Hi I'm getting closer: Please see: http://www.businessmobiles.com/comcalc/test4.php All I need to do now is work out how to keep adding to the array. At the moment each time I select from the drop down list, it overwrites the old array. Any help please? New code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> <script> function notes(){ var nList = document.getElementById("notesDropdown").value; var city = []; city[0] = nList; var msg = ""; for (x = 0; x < city.length; x++) { msg = msg + city[x] + ", ";; } document.getElementById("finalNotes").value=msg; } </script> </head> <body> <form name="finalNotesList" action="test4.php" method="post"> <select name="notesDropdown" size="0" onchange="notes();" id="notesDropdown" style="width:220px; font-size:10px;" > <option value=""> </option> <option value="Number port on Line 1">Number port on Line 1</option> <option value="Add International Roaming to Line">Add International Roaming to Line</option> <option value="Add International Call">Add International Call</option> <option value="Upgrade on Line">Upgrade on Line</option> </select> <br><br> <textarea name="finalNotes" rows="3" cols="75" id="finalNotes"></textarea> <br><br> <input type="submit" name="finalNotesList" value="Submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Adam Posted June 23, 2009 Share Posted June 23, 2009 I've not looked much at the code, but think I've spotted the problem. Try moving "var city = [];" outside of the function. As it is, every time that function's called it's declaring 'city' as an empty array, which is probably why you're not seeing what you expect. This may not be the solution though just a quick idea.. Quote Link to comment Share on other sites More sharing options...
spires Posted June 23, 2009 Author Share Posted June 23, 2009 Hi Thanks for your help. I'm sure your on the right track, but it did not work. The array keeps getting reset every time I select a new option. Up to date code: <script> var city = []; var msg = ""; function notes(){ var nList = document.getElementById("notesDropdown").value; var cLength = city.length + 1; for (x = 0; x < cLength; x++) { city[x] = nList; msg = city[x] + ", "; } document.getElementById("finalNotes").value=msg; } </script> Any more advice would be great Quote Link to comment Share on other sites More sharing options...
Adam Posted June 23, 2009 Share Posted June 23, 2009 You've removed "city[0] = nList;" ... does that not have anything to do with it? Quote Link to comment Share on other sites More sharing options...
spires Posted June 23, 2009 Author Share Posted June 23, 2009 It does, but I need each time an option is selected, it stores it into the array. So this is what i'm doing now: city[city.length] = nList; If 1 value is in the array, this will become city[0], However, the count is 1, which means on the next pass the array will become city[1], and the count will be 2. This seems to be working ok, the array is storing the values, but I can't seem to display every value in the array now. I thought this would be easy, no such luck <script> var city = []; var msg = ""; function notes(){ var nList = document.getElementById("notesDropdown").value; city[city.length] = nList; alert(city.length); for (x = 0; x < city.length; x++) { msg = city[x] + ", "; } document.getElementById("finalNotes").value=msg; } </script> Quote Link to comment Share on other sites More sharing options...
spires Posted June 23, 2009 Author Share Posted June 23, 2009 Solved var city = []; function notes(){ var nList = document.getElementById("notesDropdown").value; city[city.length] = nList; var msg = ""; for (x = 0; x < city.length; x++) { document.getElementById("test").value = x; msg += city[x] + ", "; } document.getElementById("finalNotes").value=msg; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.