Search code examples
javascripthtmlinputvanilla-forums

getting value from input and displaying in a div


I am trying to get the value of an input and display it in a div under it, while the user is typing. and i want to do it with vanilla js. no j query.

MY HTML:

<div id="userInputBox">
<input id="inputText" type="text"      
placeholder="Enter your question" size = "50" onkeyup="myDisplay()"/>
</div>
<div class="displayQuestion"></div>

MY JS:

var input = document.getElementById('inputText').value;
var showQuestion = document.getElementsByClassName('displayQuestion');
function myDisplay(e){
showQuestion.innerHTML = input;
}

Solution

  • Try this:

    function myDisplay(e) {
      var input = document.getElementById('inputText').value;
      var showQuestion = document.getElementById('displayQuestion');
      showQuestion.innerHTML = input;
    }
    <div id="userInputBox">
      <input id="inputText" type="text"
    placeholder="Enter your question" size = "50" onkeyup="myDisplay(this.value)"/>
    </div>
    <div id="displayQuestion"></div>