0

I am trying to display multiple user outputs, so that when something is entered in the text field, it is captured and displayed in the div.

At the moment it keeps overwriting with the new result rather than adding to what is already displayed.

Can anyone help with this?

HTML:

<!DOCTYPE HTML>

<html lang="en">
<!-- more meta data needs to go in here -->
<head>
    <script type="text/javascript"  src="main_app_javascript.js"></script>
    <link rel="stylesheet" type="text/css" href="">
    <meta charset="UTF-8">
    <title>List Check</title>
</head>

<body>
    <input type="text" id="enter_box">
    <button onclick="output_function()"> Add To List </button>

    <div id="list_output"></div>

</body>

JavaScript:

function output_function() {
    var user_input = document.getElementById("enter_box").value;
    document.getElementById("list_output").innerHTML = user_input;
}
MarthyM
  • 1,839
  • 2
  • 21
  • 23
user3355961
  • 725
  • 3
  • 16
  • 34
  • possible duplicate of [How to append data to div using javascript?](http://stackoverflow.com/questions/5677799/how-to-append-data-to-div-using-javascript) – Dhiraj May 31 '15 at 14:44

2 Answers2

0

Do

document.getElementById("list_output").innerHTML += user_input + '<br />';

instead.

This will concatenate your value and add a line break at the end of the text, to create a "list". Notice the =+ and + '<br /> differences.

Norman Breau
  • 2,132
  • 16
  • 35
0

You could also try this

    function output_function() {
        'use strict';
        var user_input  = document.getElementById("enter_box").value;
        var list_output = document.getElementById("list_output");

        if( list_output.innerHTML === '' ) {
            list_output.innerHTML = '<p>' + user_input + '</p>';
        } else {
            list_output.innerHTML += '<p>' + user_input + '</p>';
        }
    }
kellymandem
  • 1,709
  • 3
  • 17
  • 27