Ken
Ken

Reputation: 65

Outputting text into input field in JavaScript

Trying to press a button which outputs text in an input field. the function is to output text "x" into input field upon press, which is set to read only. only js and html allowed. Here's what i got so far in HTML and js:

 <button id="button4" onclick="output()">Hokus Pokus</button>
 <input id="printoutput" readonly="true" type="text">

js:

function output() {

document.getElementById("printoutput").innerHTML = "x";

}

Why does this not work?

Upvotes: 2

Views: 1692

Answers (3)

Ken
Ken

Reputation: 65

Fixed. did this:

function output() {

document.getElementById("printoutput").innerHTML = "x";

}

When it should be:

function output() {

document.getElementById("printoutput").value = "x";

}

Upvotes: 1

epanalepsis
epanalepsis

Reputation: 935

Do it like this and it works like a charm:

function output() {

document.getElementById("printoutput").value = "x";

}
 <button id="button4" onclick="output()">Hokus Pokus</button>
<input id="printoutput" readonly="true" type="text">

Upvotes: 2

hawkstrider
hawkstrider

Reputation: 4341

You need to set the value of the input, not the inner html

document.getElementById("printoutput").value = "x";
<button id="button4" onclick="output()">Hokus Pokus</button>
 <input id="printoutput" readonly="true" type="text">

Upvotes: 0

Related Questions