Click here to Skip to main content
15,888,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
XML
<!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
v3

1 solution

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').
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900