Click here to Skip to main content
13,150,539 members (34,638 online)
Rate this:
Please Sign up or sign in to vote.
See more:
The code creates an array then sets it to a text-area.
I then return it turn split it back to an array sort it and attempt to replace the commas with new lines and return it to another text-area.
However I can't replace the commas or set the second text-area with the values.
Does anyone have any ideas? I really appreciate it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <title>Grocery List</title>
    <script type="text/javascript">
		function addItem() {
            var myForm = document.forms.groceryList;
            var listAdd = [];
            for (i = 0; i < myForm.category.length; i++) {
                if (myForm.category[i].checked) {
            type = myForm.category[i].value;
            var myItem = document.getElementById("itemName").value;
        	listAdd.push(type + " : "  + myItem + "\n");
			var area = document.getElementById('list');
			//add array values to text area as a string
			area.value = listAdd + area.value;
function printSortedList()
	var myForms = document.forms.groceryList;
	newList = document.getElementById('list')
	newValue = newList.value;
	//pattern = /\n/g,',';
	//newSort = newSort.replace(/\n/g,',');
   newSort = newValue.split('\n');
   //alphaList = new Array;
   alphaList = alphaValue.value;
	alphaSort = alphaList.replace(/,/g, "\n");
   //pattern = /,/g,'\n';
   //for ( i = 0; i < newSort.length; i++) {
   //alhpaList[i] = alphaSort[i].replace (pattern)
	//x = myForm.list.length;
	//newAlpha = newSort.sort();
	//newValue = newAlpha.replace(/,/g,'\n');
	//listAlpha = newValue.split(',');
   // alert(alphaSort);
	/*alphaAdd = newArray;
	for (j=o;i<myforms.list.length;j++)>
		alphaAdd.push(alphaSort[j] + "\n");
	var tArea = document.getElementById('sorted');
	tArea.value = alphaSort;
	//document.getElementById('list').value = alphaSort;
<style type="text/css">
  body,td,th {
	color: #00F;
body {
	background-color: #E4E4E4;
	background-image: url(grocery_bag.jpg);
  <form action="#" name="groceryList">
  Category: <input type="radio" name="category" value="Produce" />Produce
  <input type="radio" name="category" value="Meat and Cheese" />Meat and Cheese
  <input type="radio" name="category" value="Grocery" />Grocery
  <input type="radio" name="category" value="Beverage" />Beverage
  <br />
  Item: <input name="item" type="text" id="itemName" size="30" />
  <br />
  <input type="button" name="add" value="Add to List"  önclick="addItem()"/>
  <br />
 <textarea name="list" rows="30" id="list" style="width:500" size="15"></textarea>
 <textarea name="sorted" rows="30" id="sorted" style="width:500" size="15"></textarea>
<br />  
  <input type="button" id="button" name="sort" value="Print Sorted List"
Posted 15-Dec-12 1:32am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The first thing that stands out is that sort() sorts the members of the original array so you don't need to assign the result to another array.

Converting an array to a string performs a join with a comma as the separator - the easiest way to use a "\n" instead of a comma is to explicitly use theArray.join('\n').

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100