0

This is the HTML code:

<body>
  <form>
    <input id="input" type="text" name="input" value="Enter Here">
    <input type="submit" value="Submit">
  </form>
  <div id="display">
  </div>
</body>

This is the JavaScript:

input = document.getElementById("input");
if (input.value == "Hello") {
  display.innerHTML = "Hello";
} else {
  display.innerHTML = "Type";
}

When I change the input value by clicking on the input field and typing "Hello", it does not display "Hello" in display.innerHTML. I would like it to display "Hello" when "Hello" is typed into the input field. That's a lot of "Hello"'s! Any help would be great! Thanks in advance.

2
  • display= document.getElementById("display");? Commented Dec 14, 2016 at 16:23
  • When are you running this JavaScript? On page load? Then it will run before the user has a chance to type anything, but never again. Commented Dec 14, 2016 at 16:23

3 Answers 3

1

var input = document.getElementById("input"),
  display=document.getElementById("display");
input.oninput=function(){
  if (input.value === "Hello") {
  display.innerHTML = "Hello";
} else {
  display.innerHTML = "Type";
}
};
    <input id="input" type="text" name="input" value="Enter Here">
  <div id="display">
  </div>

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

Comments

0

Your javascript code only gets executed once before you have entered anything in the input field.

You need to either setup a change handler for the input field or a submit handler for the form and set display.innerHTML.

Also, did you miss a display = document.getElementById("display");?

Comments

0

If you want use your button for submit the value of your textbox (your input type text-field) use onclick event as follows:

function displayData() {
  var div_display = document.getElementById('display');
  /* This is your input, but you shoud use another Id for your fields. */
  var textValue = document.getElementById('input').value;
  

  /* Change the inner HTML of your div. */
  div_display.innerHTML = textValue;
}
<input id="input" type="text" name="input" value="Enter Here" />
<input type="submit" value="Submit" onclick="displayData();" />

<div id="display">
</div>

Hope it helps.

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.