tags:

views:

45

answers:

6

I'm having some trouble getting my code to work. This is what I have so far.

function outputNamesAndTotal() {
  var name;
  var outputTable;
  var inputForm;
  var nameArray;
  var outputDiv;

  outputDiv = document.getElementById("outputDiv");
  inputForm = document.getElementById("inputForm");
  outputTable = document.getElementById("outputTable");
  name = inputForm.name.value;
  nameArray = [];

  nameArray.push(name);


  for (var i = 0; i > nameArray.length; i++) {

    outputTable.innerHTML += "<tr>" + nameArray[i] + "</tr>";

  }    

  inputForm.name.focus();
  inputForm.name.select();


  return false;

}

When I add the loop it breaks the code completely, but I can't figure out why.

What I'm trying to do is use an HTML form to get a name from the user. Once the user enters the name, the program adds the name to the array, and outputs each array entry to a row in a table.

It's pretty basic, but it's still giving me all kinds of trouble!

+2  A: 

for (var i = 0; i > nameArray.length; i++) {

I think you mean i < nameArray.length

Josh Kerr
A: 

Still need help to figure this one out. I'm so close, yet so far way. :(

javanewb123
+2  A: 

I think you are clearing your array of names every time you call the function. You should bring the line:

nameArray = [];

out and make it global.

I ran a quick test and the following code works in at least FireFox

Edited to use appendChild

<html>
<head>
<script type='text/javascript'>
var names = [];
function addName() {
    var nameTxt = document.getElementById('name_txt');
    var name = nameTxt.value;
    names.push(name);
    var outTable = document.getElementById('out_tbl');

    var row = document.createElement('tr');
    var entry = document.createElement('td');
    var txt = document.createTextNode(name);

    entry.appendChild(txt);
    row.appendChild(entry);
    outTable.appendChild(row);


    var numDiv = document.getElementById('num_div');
    removeAllChildren(numDiv);
    var numTxt = document.createTextNode('You have ' + names.length + ' names');
    numDiv.appendChild(numTxt);
}
function removeAllChildren(e) {
    while (e.hasChildNodes()) {
     e.removeChild(e.firstChild);
    }
}
</script>
</head>
<body>
    <table id='out_tbl'>
    </table>
    <div id='num_div'>You have 0 names</div>
    <input id='name_txt' type='text'/>
    <button onclick="addName()">CLICK</button>
</body>
</html>

Edit: Oh yeah and you are the fact that you are looping through the array every time. If you "globalize" the name array, you're gonna print the whole array every time you add a name.

Edit x2: the code you originally posted had nameArray as a local variable inside the function. This effectively clears the array every time you call the function. Then every time you call the function you add the current name to the now empty array, and loop through all 1 (one) elements that the array now holds.

What you want to do is "globalize" the name array, and remove the loop from your function. This will allow you to build up your name array across multiple calls, and works the way that you want it.

Also, innerHTML is not really the best way to add things to the page. I would suggest using appendChild().

-C

goatlinks
I'm required to use innerHTML for this assignment. I'm still pretty new to javascript and coding in general.I got the code to work, but only until I try to display the total number of names in the array. When I try to add code to display the total, it breaks the outputTable.innerHTML code and refreshes the page after I try to enter a second name. When I enter the first name it just shows the total output. I'm not sure why it would refresh the second time but not the first.
javanewb123
Oh okay that makes sense. Can you put the number of names in a separate div like my example? If so, just get that div (lets call it numDiv) and call numDiv.innerHTML = "You have " + nameArray.length + " names";
goatlinks
A: 

When i moved the array to a global variable it started to output the entire array each time I entered another name.

The last thing I have to do and the reason for the outputDiv = document.getElementById("outputDiv"); is display the total number of names in the array.

outputDiv.innerHTML = "Total number of names: " + nameArray.length; breaks the code however.

This is the code I have now -

function outputNamesAndTotal() {
var name;
var outputTable;
var inputForm;
var nameArray = [];
var outputDiv;

outputDiv = document.getElementById("outputDiv");
inputForm = document.getElementById("inputForm");
outputTable = document.getElementById("outputTable");
name = inputForm.name.value;


nameArray.push(name);


for (var i = 0; i < nameArray.length; i++) {

outputTable.innerHTML += "<tr>" + nameArray[i] + "</tr>";

}
outputDiv.innerHTML = "Total number of names: " + nameArray.length; inputForm.name.focus(); inputForm.name.select();

return false;

}

javanewb123
A: 

Goatlinks, I misunderstood what you said at first. You are right about resetting it.

var nameArray = [];
function outputNamesAndTotal() {
var name;
var outputTable;
var inputForm;

var outputDiv;

outputDiv = document.getElementById("outputDiv");
inputForm = document.getElementById("inputForm");
outputTable = document.getElementById("outputTable");
name = inputForm.name.value;


nameArray.push(name);





outputTable.innerHTML += "<tr> <td>" + nameArray[nameArray.length] + "</td> </tr>";




inputForm.name.focus();
inputForm.name.select();

return false;

}

I tried to change it to that, but now it just comes up undefined each time.

javanewb123
See my latest edit. I think i cleared it up a bit. Also I suggested you use appendChild instead of adding to innerHTML. This is a more "xml" like way of doing it, and i think it preserves the DOM better. And plus you can do this for more than just the table. You can make the number of names part work as well. (I'd put the number of names in its own div anyway just to keep them separate)-C
goatlinks
Oh, I see the problem. I bet outputDiv CONTAINS outputTable. Am I right? If so setting outputDiv.innerHTML overwrites the whole table. You should add a new div that is a SIBLING (in xml-speak) to outputTable, and set ITS innerHTML to your length string.
goatlinks
You are a gentleman and a scholar. Works like a charm. <3
javanewb123
Awesome! i'm glad I could help!
goatlinks
A: 

Okay, I almost got the program to do what I need to do with this code -

var nameArray = [];
var count = 0;

function outputNamesAndTotal() {
var name;
var outputTable;
var inputForm;

var outputDiv;

outputDiv = document.getElementById("outputDiv");
inputForm = document.getElementById("inputForm");
outputTable = document.getElementById("outputTable");
name = inputForm.name.value;


nameArray.push(name);






outputTable.innerHTML += "<tr> <td>" + nameArray[count]
    + "</td> </tr>";

count++;



inputForm.name.focus();
inputForm.name.select();

return false;

}

That is, everything besides displaying the total number of names. I tried to add

outputDiv.innerHTML = "Total number of names: " + count;

But not only does that stop the program from displaying the array, it also reloads the page each time.

javanewb123