Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
    <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) {
                    break;
                }
            }
            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,',');
   //alert(newValue);
   newSort = newValue.split('\n');
   
   //alert(newSort);
   alphaValue=newSort.sort();
   alert(alphaValue);
   //alphaList = new Array;
   alphaList = alphaValue.value;
	alphaSort = alphaList.replace(/,/g, "\n");
   alert(alphaSort);
   //pattern = /,/g,'\n';
   //for ( i = 0; i < newSort.length; i++) {
 
   
   //alhpaList[i] = alphaSort[i].replace (pattern)
   //}
	//x = myForm.list.length;
	//alert(x);
	
	
	//newAlpha = newSort.sort();
	//alert(alphaList);
	//newAlpha.toString();
	//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");
	}
	alert(alphaAdd);
	*/
	var tArea = document.getElementById('sorted');
	
	tArea.value = alphaSort;
	//document.getElementById('list').value = alphaSort;
}
</script>
<style type="text/css">
  body,td,th {
	color: #00F;
}
body {
	background-color: #E4E4E4;
	background-image: url(grocery_bag.jpg);
}
</style>
  </head>
  <body>
  <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"
 önclick="printSortedList()"/>
  </form>
  </body>
</html>
Posted 15-Dec-12 1:32am
v3

1 solution

Rate this: bad
good
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').
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 330
1 BillWoodruff 314
2 George Jonsson 274
3 CPallini 265
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
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