Willem van der Veen
Willem van der Veen

Reputation: 36610

HTML input button changing content

I'm trying to change the contents of an input tag using a JS function but nothing I tried seems to work. Here's what I tried:

function go (){
document.getElementById("input").innerHTML.value =  5;
};
<body>

<input type="number" id="input">

<button onclick="go()">go</button>

</body>

Would really appreaciate it if someone show me how this is done

Upvotes: 0

Views: 40

Answers (2)

Duncan Thacker
Duncan Thacker

Reputation: 5188

You don't want to change the innerHTML, just the value of the input element (which is an attribute). So this should work. I changed to a string because that's what attributes usually are, but it shouldn't matter.

document.getElementById("input").value =  "5";

Upvotes: 1

Dij
Dij

Reputation: 9808

just use document.getElementById("input").value

function go (){
   document.getElementById("input").value =  5;
};
<body>

<input type="number" id="input">

<button onclick="go()">go</button>

</body>

Upvotes: 4

Related Questions