0

Hello I am here with a quick question on sending user input to a div section of an html document. I asked this question earlier and it seemed to be too broad so I'm going to try to be more specific this time.

I am attempting to send a user input to the div onclick of the send button but every time the code is simply changing the text rather than printing the next text under it. I'm curious what I'm doing wrong with this. Thanks for reading and here's my code.

<div id="out"</div>
<input type="text" name="textIn" id="txtin">
<input type="button" value="Hit me" onclick="hello()"></input>
<script>
 function hello() {
    document.getElementById("out").innerHTML = 
    document.getElementById('txtin').value + "<br />"
}
</script>

https://jsfiddle.net/su0o83hj/1/

2
  • 3
    <div id="out"</div> - Is that your actual code? Commented Jun 13, 2017 at 14:40
  • Fix this first: <div id="out"></div> then @Jay Buckman answered correctly Commented Jun 13, 2017 at 14:44

3 Answers 3

1

If you want to append to the existing text, use += instead of = in the function:

function hello() {
    document.getElementById("out").innerHTML += 
    document.getElementById('txtin').value + "<br />"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much i will try to remember this in the future :D
0

You have some syntax errors, fix it and it will works

<div id="out"></div>
<input type="text" name="textIn" id="txtin" />
<input type="button" value="Hit me" onclick="hello()" />
<script>
 function hello() {
    document.getElementById("out").innerHTML += 
    document.getElementById('txtin').value + "<br />"
}
</script>

Comments

0

You can use Element.insertAdjacentHTML() passing the first parameter beforeend:

var out = document.getElementById('out'),
    txtin = document.getElementById('txtin');
    
function hello() {
  out.insertAdjacentHTML('beforeend', txtin.value + '<br>');
}
<div id="out"></div>
<input type="text" name="textIn" id="txtin">
<input type="button" value="Hit me" onclick="hello()">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.