0

So this is probably an easy one, but I'm just not doing it right. My goal is to send the user input from this textbox:

<input type='text' placeholder='Form Name...' id='formNameInput' required>

Into this Div:

<div id="code_output"></div>

I'm trying to make it appear in real time, and so far I used this to try and do so, but it doesn't work:

document.getElementById("code_output").innerHTML += document.getElementById("formNameInput").value;

Why doesn't it show? Does my code need something to trigger the Javascript?

4
  • working for me, do you have any console error? do you want to show value in div after any trigger ? Commented Mar 19, 2018 at 18:07
  • in real time. You need to use onkeyup Commented Mar 19, 2018 at 18:08
  • " in real time" so you need a change event or keyup on your input. Commented Mar 19, 2018 at 18:09
  • Where do you call that line of JavaScript? It does not magically keep running, you need to tell it to run. Commented Mar 19, 2018 at 18:09

4 Answers 4

2

You're close, but the issue is that you're not using an event handler. The script is executing your code once, as soon as possible (before you have the chance to enter anything into the text input). So, you have to add some sort of event listener so that the copying happens at the appropriate time. Something like below:

document.getElementById('formNameInput').addEventListener('keyup', copyToDiv);

function copyToDiv() {
  document.getElementById("code_output").innerHTML = document.getElementById("formNameInput").value;
}
<input type='text' placeholder='Form Name...' id='formNameInput' required>

<div id="code_output"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

Use input instead of keyup.
Thanks this worked like wonders. I also went and added onkeyup='myfunction()' to my input like someone else said and turned my JS code into a function and it worked as well. But whoever gave that answer deleted it before I could accept it.
0

You need to do that whenever the value of formNameInput changes. For that you need an event.

Your code should look like:

document.getElementById("formNameInput").addEventListener('input', function () {
   document.getElementById("code_output").innerHTML += this.value;
});

Comments

0
function change() {
  document.getElementById("code_output").innerHTML = document.getElementById("formNameInput").value;
}

document.getElementById('formNameInput').onkeyup = change

maybe this is what you are trying?

Comments

0

You need to attach an event listener to your input that executes a function any time an input event occurs on the field:

formNameInput.addEventListener('input', function(e) {
  code_output.textContent = e.target.value
})
<input type="text" placeholder="Form Name..." id="formNameInput" required />
<div id="code_output"></div>

Please note that the above code takes advantage of the fact that browsers automatically create a global variable for each element with a unique id attribute value, and this variable has the same name as the value of the id.

If the concept of events is new to you, this might be a good place to get started:

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events

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.