1

getElementById innerHTML not work for me ?

When user fill data in input tag, it's will be change data to Hello!

http://jsfiddle.net/59DFY/21/

<input type="text" id="hola" onchange="changeMe()">
<script>
function changeMe() {
  document.getElementById("hola").innerHTML="Hello!";
}
</script>
robert dewo
  • 33
  • 1
  • 5

2 Answers2

3

Use value property of input text.

The value property sets or returns the value of the value attribute of a text field.

The value property contains the default value OR the value a user types in (or a value set by a script).

So try to Change it like this

function changeMe() {
  document.getElementById("hola").value ="Hello!";
}

http://jsfiddle.net/59DFY/21/

Mansoorkhan Cherupuzha
  • 1,761
  • 1
  • 24
  • 45
2

You need to use value. It will "change" the inner HTML on the input element, but that's not displayed on the screen since it's a void element. It does not technically have innerHMTL but browsers will do something like <input ...>Hello!</input>. The text for an input element uses value other than innerHTML that for example an <p> or <div> would use.

function changeMe() {
    document.getElementById("hola").value="Hello!"; 
}
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • 1
    "*It will change the inner HTML on the input element*" - no, it won't. The problem is specifically that the `` element is a void, or replaced, element: it *has no inner HTML*, it cannot contain any child-nodes of any type. – David Thomas Dec 20 '14 at 14:09
  • @DavidThomas Actually it does. Changing inner HTML on a `input` would make it look like so: `Hello!`. But it's not displayed on the screen. – Spencer Wieczorek Dec 20 '14 at 14:11
  • And the reason it doesn't display on the screen is because: "the element is a void, or replaced, element: it *has no inner HTML*, it cannot contain any child-nodes of any type." Assigning a property (`innerHTML`) doesn't confer any ability, or validity, to the solution you propose. – David Thomas Dec 20 '14 at 14:13
  • @DavidThomas I think I see what you mean, when I said "inner HTML" I wasn't talking about the property, but what the browser did. I've changed my answer so i'ts more clear. – Spencer Wieczorek Dec 20 '14 at 14:21
  • @robert dewo it works if you see it by inspecting element.your code is correct. – Raham Dec 20 '14 at 14:31