0
<input name="searchTxt" type="text" maxlength="512" id="searchTxt" class="searchField"/>
<p id="text"></p>
<script>
var input = document.getElementById("searchTxt");
document.getElementById('text').innerHTML = input.value;</script>

I'm typing in the input and the text should appear below,but it doesn't and I dont know why, does anybody know what the problem might be?

2
  • Your script is running when the page is loaded, not when the user types into the search field. Commented Mar 3, 2015 at 3:22
  • What does "checking math function" have to do with this question? There's no math anywhere. Commented Mar 3, 2015 at 3:28

2 Answers 2

1

If you want to run the script when the user types into the field, you need to put it into an event listener. Your code is just running once, when the page is first loaded. Since nothing has been entered into the text field yet, nothing is put into the innerHTML.

document.getElementById("searchTxt").addEventListener('keyup', function() {
  document.getElementById('text').innerHTML = this.value;
});
<input name="searchTxt" type="text" maxlength="512" id="searchTxt" class="searchField" />
<p id="text"></p>

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

Comments

0

Attach the oninput event handler to the input element like this:

var input = document.getElementById("searchTxt");

input.oninput = function() {
    document.getElementById('text').innerHTML = input.value;    
}

input.onpropertychange = input.oninput; // for IE8 and below

JSFiddle

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.