0

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

2 Answers 2

4

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>

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

Comments

1

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";

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.