38

So, I am trying to print out an array that gets user input text added to it, but what I want to print out is an ordered list of the array. As you can see, (if you run my code) the list item just keeps getting the user input added to it, and no new list items are added with people's names.

Here is the code below:

 <!DOCTYPE html>
 <html>
 <head>
 First name: <input type="text" id="firstname"><br> 

 <script type="text/javascript">
 var x= [];

 function changeText2(){
var firstname = document.getElementById('firstname').value;
document.getElementById('boldStuff2').innerHTML = firstname;
x.push(firstname);
document.getElementById('demo').innerHTML = x;
 }
 </script>

 <p>Your first name is: <b id='boldStuff2'></b> </p> 
 <p> Other people's names: </p>
 
 <ol>
     <li id = "demo"> </li> 
 </ol>

 <input type='button' onclick='changeText2()'   value='Submit'/> 

 </head>
 <body>
 </body>
 </html>
Syscall
  • 19,327
  • 10
  • 37
  • 52
user2581598
  • 385
  • 1
  • 3
  • 8
  • 1
    `` only contains meta information and links to resources, not actual content. But, the reason why you don't get a new list item is that you are not creating one. – Felix Kling Jul 21 '13 at 15:39

4 Answers4

85

If you want to create a li element for each input/name, then you have to create it, with document.createElement [MDN].

Give the list the ID:

<ol id="demo"></ol>

and get a reference to it:

var list = document.getElementById('demo');

In your event handler, create a new list element with the input value as content and append to the list with Node.appendChild [MDN]:

var firstname = document.getElementById('firstname').value;
var entry = document.createElement('li');
entry.appendChild(document.createTextNode(firstname));
list.appendChild(entry);

DEMO

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I would have exptected that adding text to li would be simpler, with something like entry.text ='foo'. Why do I need a `textnode`? – Timo Jan 17 '21 at 13:34
  • 1
    @Timo: It's been a while since I wrote that answer. I can't remember why I did it that way. But yes, you can absolutely do `entry.textContent = firstname;`. – Felix Kling Jan 17 '21 at 16:44
  • Thanks Felix, I did a nested list based on your code, like ul-li-ul-li-/li-/ul-/li-/ul. I can imagine there is recursion for this when the list gets bigger, do you think a question here makes sense? – Timo Jan 17 '21 at 19:43
  • 1
    @Timo: I'd look around first. I'd imagine it has been asked before how to created a nested list from data. – Felix Kling Jan 17 '21 at 19:46
  • [here](https://github.com/tik9/tik9.github.io/blob/main/t.html) is an example that could be improved.[here](https://github.com/tik9/tik9.github.io/blob/main/t2.html) is a nested loop but not what I want. I will go for the fquestion tomorrow. – Timo Jan 17 '21 at 19:56
6

Try something like this:

var node=document.createElement("LI");
var textnode=document.createTextNode(firstname);
node.appendChild(textnode);
document.getElementById("demo").appendChild(node);

Fiddle: http://jsfiddle.net/FlameTrap/3jDZd/

Josh
  • 2,835
  • 1
  • 21
  • 33
5

I was recently presented with this same challenge and stumbled on this thread but found a simpler solution using append...

var firstname = $('#firstname').val();

$('ol').append( '<li>' + firstname + '</li>' );

Store the firstname value and then use append to add that value as an li to the ol. I hope this helps :)

Jesse Novotny
  • 704
  • 7
  • 16
1

The above answer was helpful for me, but it might be useful (or best practice) to add the name on submit, as I wound up doing. Hopefully this will be helpful to someone. CodePen Sample

    <form id="formAddName">
      <fieldset>
        <legend>Add Name </legend>
            <label for="firstName">First Name</label>
            <input type="text" id="firstName" name="firstName" />

        <button>Add</button>
      </fieldset>
    </form>

      <ol id="demo"></ol>

<script>
    var list = document.getElementById('demo');
    var entry = document.getElementById('formAddName');
    entry.onsubmit = function(evt) {
    evt.preventDefault();
    var firstName = document.getElementById('firstName').value;
    var entry = document.createElement('li');
    entry.appendChild(document.createTextNode(firstName));
    list.appendChild(entry);
  }
</script>