Help with nodes

I am trying to find away to add items from an array to a table, then to add new item to the array and add those as well.  On this project, the contributor and total boxes do what they are supposed to, but entries are not added to the contributor list.  I assume the problem is with my addRow() function, but I can't pinpoint it.  The program contains three files; clist.htm, newdata2.js (functions), and data.js(arrays):

html>
<head>

   Filename:         clist.htm
   Supporting files: data.js, lhouse.css, logo.jpg, newdata2.js

-->
<title>The Lighthouse</title>
<link href="lhouse.css" rel="stylesheet" type="text/css" />

<script src="data.js" type="text/javascript"></script>
<script src="newdata2.js" type="text/javascript"></script>

</head>

<body onload = "makeTable()">
<form name="donations" id="donations" action="">
<div id="title">
   <img src="logo.jpg" alt="The Lighthouse" />
   The Lighthouse<br />
   543 Oak Street<br />
   Delphi, KY &nbsp;&nbsp;89011<br/>
   (542) 555-7511
</div>

<div id="data_entry">
   <table>
   <tr>
      <td>Contributors</td>
      <td id="totalContr"></td>
   </tr>
   <tr>
      <td>Total</td>
      <td id="totalAmt"></td>
   </tr>
   </table>

   <table>
   <tr><td colspan="2"><h3>Enter a New Contributor</h3></td></tr>
   <tr>
      <td>First Name</td><td><input name="fname" id="fname" /></td>
   </tr>
   <tr>
      <td>Last Name</td><td><input name="lname" id="lname" /></td>
   </tr>
   <tr>
      <td>Street</td><td><input name="street" id="street" /></td>
   </tr>
   <tr>
      <td>City</td><td><input name="city" id="city" /></td>
   </tr>
   <tr>
      <td>State</td>
      <td><select name="state" id="state">
      <option>AL</option><option>AK</option><option>AZ</option><option>AR</option>
      <option>CA</option><option>CO</option><option>CT</option><option>DE</option>
      <option>DC</option><option>FL</option><option>GA</option><option>HI</option>
      <option>ID</option><option>IL</option><option>IN</option><option>IA</option>
      <option>KS</option><option>KY</option><option>LA</option><option>ME</option>
      <option>MD</option><option>MA</option><option>MI</option><option>MN</option>
      <option>MS</option><option>MO</option><option>MT</option><option>NE</option>
      <option>NV</option><option>NH</option><option>NJ</option><option>NM</option>
      <option>NY</option><option>NC</option><option>ND</option><option>OH</option>
      <option>OK</option><option>OR</option><option>PA</option><option>RI</option>
      <option>SC</option><option>SD</option><option>TN</option><option>TX</option>
      <option>UT</option><option>VT</option><option>VA</option><option>WA</option>
      <option>WV</option><option>WI</option><option>WY</option>
      </select>
      </td>
   </tr>
   <tr>
      <td>Zip</td><td><input name="zip" id="zip" /></td>
   </tr>
   <tr>
      <td>Amount</td><td><input name="amount" id="amount" /></td>
   </tr>
   <tr>
      <td>Date</td><td><input name="date" id="date" /></td>
   </tr>
   <tr>
      <td colspan="2" id="buttoncell1">
         <input type="button" onclick="addRecord()" value="Add Contributor" />
      </td>
   </tr>
   </table>
</div>

<div id="report">
   <p><input value="Remove Checked Items" type="button" onclick="removeRecords()"/></p>

   <table id="ctable">
   <tr><th colspan="5">Contributor List</th>
   </tr>
   <tr id="titleRow">
      <th>Remove</th>
      <th>Date</th>
      <th>Name</th>
      <th>Address</th>
      <th>Amount</th>
   </tr><tr id="emptyRow"><td class="rc"><input type="checkbox" /></td><td></td><td></td><td></td><td class="ac"></td></tr>
   </table>
</div>

</form>

</body>
</html>

Open in new window


function updateTotals() {
	var totalContr = document.getElementById("totalContr");
	totalContr.innerHTML = amount.length;
	var totalContributions = 0;
	for (var i=0; i <amount.length; i++) {
	    totalContributions += parseInt(amount[i]);	
	}
	document.getElementById("totalAmt").innerHTML = totalContributions;
}


function makeTable() {
   	var headingRow = document.getElementById("titleRow");
	var blankRow = document.getElementById("emptyRow");
	dataRow = blankRow.cloneNode(true);
	for (i=0; i<amount.length; i++) {
	    addRow(i);	
	} 
	updateTotals();
}



function addRow(index) {
    var newRow = dataRow.cloneNode(true);
    newRow.childNodes[1].innerHTML = date[index];
    newRow.childNodes[2].innerHTML = lastName[index] + ", " + firstName[index];
    newRow.childNodes[3].innerHTML = street[index] + "<br>" + city[index] + ", " + state[index] + zip[index];
    newRow.childNodes[4].innerHTML = amount[index];
	if (index = 0) { 
		blankRow.parentNode.replaceChild(newRow, blankRow)
	 }
		if (index != 0) { 
		headingRow.parentNode.insertBefore(newRow, headingRow.nextSibling)
		}
}

function addRecord() {
	recordNum = amount.length; 
	firstName[recordNum] = document.getElementById("fname").value; 
	lastName[recordNum] = document.getElementById("lname").value;
	street[recordNum] = document.getElementById("street").value;
	city[recordNum] = document.getElementById("city").value;
	state[recordNum] = document.getElementById("state").value;
	zip[recordNum] = document.getElementById("zip").value;
	amount[recordNum] = document.getElementById("amount").value;	
	date[recordNum] = document.getElementById("date").value;
	addRow[recordNum];  
	updateTotals();
	
}

function removeRecord(index) {
	firstName.splice(index,1);
	lastName.splice(index,1);
	street.splice(index,1);
	city.splice(index,1);
	state.splice(index,1);
	zip.splice(index,1);
	amount.splice(index,1);
	date.splice(index,1);
}
 
function removeNode(node) {
	var prevNode = node.previousSibling;
	prevNode.parentNode.removeChild(prevNode);     
	return prevNode;
}
 
var headerRow;  
var recordNum;
function removeRecords() {
	document.getElementById("titleRow") = headerRow;
	recordNum = amount.length;
	for ( n = 0; n < headerRow.childNodes.length; n++) {
	recordNum = recordNum - 1;
	if (headerRow.childNodes[n].name = "TR") {
  	if (n.firstChild.firstChild != "") {
    removeRecord(recordNum);
	n = removeNode(n)};
	};
	updateTotals(); 
	}
}

Open in new window

/*

   Filename: data.js

   Variable List:

   firstName: Contains the first name of the contributors.
   lastName:  Contains the last name of the contributors.
   street:    Contains the contributor's street address
   city:      Contains the contributor's city of residence
   state:     Contains the contributor's state of residence
   zip:       Contains the contributor's zip code.
   amount:    Contains the amount of the contribution.
   date:      Contains the date of the contribution.

*/


firstName = new Array();
lastName = new Array();
street = new Array();
city = new Array();
state= new Array();
zip = new Array();
amount = new Array();
date = new Array()


firstName[0]="Steve";
lastName[0]="Bones";
street[0]="900 Lawton Street";
city[0]="Wheaton";
state[0]="KY";
zip[0]="89211";
amount[0]=50;
date[0]="2007-08-25";


firstName[1]="Jeri";
lastName[1]="White";
street[1]="Hawkes Lane";
city[1]="Delphi";
state[1]="KY";
zip[1]="89211";
amount[1]=150;
date[1]="2007-08-25";


firstName[2]="Tom";
lastName[2]="Thomas";
street[2]="Rigel Avenue";
city[2]="Drake";
state[2]="KY";
zip[2]="89411";
amount[2]=100;
date[2]="2007-08-27";


firstName[3]="Hugh";
lastName[3]="Warren";
street[3]="585 Lindon Court";
city[3]="Wheaton";
state[3]="KY";
zip[3]="88877";
amount[3]=50;
date[3]="2007-08-28";


firstName[4]="Lynwood";
lastName[4]="Ingersoll";
street[4]="723 Jackson Avenue";
city[4]="Delphi";
state[4]="KY";
zip[4]="88802";
amount[4]=500;
date[4]="2007-08-30";


firstName[5]="Petronila";
lastName[5]="Damico";
street[5]="44 Stewart Street";
city[5]="Drake";
state[5]="KY";
zip[5]="88604";
amount[5]=250;
date[5]="2007-08-30";


firstName[6]="Livia";
lastName[6]="Mckinnon";
street[6]="557 Ivy Avenue";
city[6]="Jasper";
state[6]="KY";
zip[6]="88960";
amount[6]=50;
date[6]="2007-08-31";


firstName[7]="Kris";
lastName[7]="Levesque";
street[7]="542 Upton Avenue";
city[7]="Delphi";
state[7]="KY";
zip[7]="88793";
amount[7]=100;
date[7]="2007-08-31";


firstName[8]="Basilia";
lastName[8]="Lu";
street[8]="851 Flad Court";
city[8]="Jasper";
state[8]="KY";
zip[8]="88633";
amount[8]=500;
date[8]="2007-09-02";


firstName[9]="Ginny";
lastName[9]="Rainey";
street[9]="657 Dawson Lane";
city[9]="Youngston";
state[9]="KY";
zip[9]="88873";
amount[9]=50;
date[9]="2007-09-03";

Open in new window

Any help is appreciated.
clist.htm
data.js
newdata2.js
Shakyjake1333Asked:
Who is Participating?
 
designatedinitializerCommented:
I tested it before posting and it works just fine.
Maybe you missed something.
Here are the files.
clist.html
data.js
newdata2.js
0
 
Shakyjake1333Author Commented:
Can anyone help please
0
 
designatedinitializerCommented:
You've got some big flaws around there.
Gimme a couple of minutes and I'll clean up your code...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
designatedinitializerCommented:
There you go.
This is you newdata2.js file revised and working.
Check the comments within the code.
Fundamental points to keep in mind:
1--ALWAYS initialize your data
2--Be careful to declare global variables OUTSIDE the functions
3--Don't mix apples with oranges (for instance, an index is not a node)
4--Do check your syntax/semantics. Semantic errors are "silent" errors, like the one you have on line 28: addRow[index] , which is NOT the same as addRow(index), but it's valid syntax.

// Globals, define them all in one place, at the top
var headingRow;
var blankRow;
var dataRow;
var headerRow;  
var recordNum;

function updateTotals() {
	var totalContr = document.getElementById("totalContr");
	totalContr.innerHTML = amount.length;
	var totalContributions = 0;
	for (var i=0; i <amount.length; i++) {
		// Check that you don't add NaN to a number!
		// It will result in NaN as well...
		num = parseInt(amount[i]);
		totalContributions += (isNaN(num)?0:num);	
	}
	document.getElementById("totalAmt").innerHTML = totalContributions;
}

// This function should not mess with any other data
// It just serves the purpose of populating the table
// with the initial data
function makeTable() {
	for (i=0; i<amount.length; i++) {
	    addRow(i);	
	} 
	updateTotals();
}

// Tow different scenarios: either this is the first row,
// in which case we should UPDATE the values in blankRow,
// or not, in which case we appendChild
function addRow(index) {
    if(i==0){
    	blankRow = document.getElementById("emptyRow");
		blankRow.childNodes[1].innerHTML = date[index];
		blankRow.childNodes[2].innerHTML = lastName[index] + ", " + firstName[index];
		blankRow.childNodes[3].innerHTML = street[index] + "<br>" + city[index] + ", " + state[index] + zip[index];
		blankRow.childNodes[4].innerHTML = amount[index];   	
    }else{
		var tableBody = document.getElementById("ctable").tBodies[1];
		var newRow = tableBody.rows[1].cloneNode(true);
		newRow.childNodes[1].innerHTML = date[index];
		newRow.childNodes[2].innerHTML = lastName[index] + ", " + firstName[index];
		newRow.childNodes[3].innerHTML = street[index] + "<br>" + city[index] + ", " + state[index] + zip[index];
		newRow.childNodes[4].innerHTML = amount[index];
		tableBody.appendChild(newRow);
	}
}

function addRecord() {
	recordNum = amount.length; 
	firstName[recordNum] = document.getElementById("fname").value; 
	lastName[recordNum] = document.getElementById("lname").value;
	street[recordNum] = document.getElementById("street").value;
	city[recordNum] = document.getElementById("city").value;
	state[recordNum] = document.getElementById("state").value;
	zip[recordNum] = document.getElementById("zip").value;
	amount[recordNum] = document.getElementById("amount").value;	
	date[recordNum] = document.getElementById("date").value;
	addRow(recordNum);  // <-- You had a silent error here (a semantic error)
	updateTotals();
}

function removeRecord(index) {
	firstName.splice(index,1);
	lastName.splice(index,1);
	street.splice(index,1);
	city.splice(index,1);
	state.splice(index,1);
	zip.splice(index,1);
	amount.splice(index,1);
	date.splice(index,1);
}


// Around here you were also making some mistakes...
function removeNode(node) {
	node.parentNode.removeChild(node);     
}
 
function removeRecords() {
	var tableBody = document.getElementById("ctable").tBodies[1];
	for ( n = 1; n < tableBody.rows.length; n++) {
		if (tableBody.rows[n].name = "TR") {
			if (tableBody.rows[n].firstChild.firstChild.value != "") {
				removeRecord(n-1);
				removeNode(tableBody.rows[n]); // <-- You were confusing indexes with nodes
			};
		};
		updateTotals(); 
	}
}

Open in new window

0
 
Shakyjake1333Author Commented:
Thank you for the help.
I entered your revisions, but all I get is a single entry in my contributor list, instead of the 8 from my data.js arrays.  Also it no longer updates my contributors or totals, and I still cannot add contributors to the list.

ANy suggestions?
0
 
Shakyjake1333Author Commented:
Working perfect, thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.