Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: javscript
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
Edited 15-Dec-12 1:43am
Tadit Dash83.2K

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
Your Filters
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 468
2 Abhinav S 305
3 sanket saxena 295
4 Tadit Dash 160
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107

Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid