Alex Carlson
Alex Carlson

Reputation: 99

Changing the value of a input box in HTML within a script

Statement In JS HTML5 I have a input box defined as

<input type="number" id="Numerator" value=40>
<input type="number" id="Denominator" value=48>

I want to change the value of these within a script.

Question: What HTML5 command will allow me to change the value of id="Numerator"?

Example

var k = 2
(insert command here to change id="Numerator" to value k)

I know how to pull the value using

document.getElementbyID("Numerator").value

but I cannot figure out how to change the value of that id.... any help would be appreciated and I could not find that value...

Upvotes: 1

Views: 81

Answers (4)

naveen atti
naveen atti

Reputation: 11

You can use like this

document.getElementById('Numerator').value = "Set Value To textbox using javascript";

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You have a typo in your getElementbyID. It should be getElementById:

var k = 2;
document.getElementById("Numerator").value = k;
<input type="number" id="Numerator" value=40>
<input type="number" id="Denominator" value=48>

Upvotes: 3

Eriks Klotins
Eriks Klotins

Reputation: 4180

https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

var el = document.getElementbyID("Numerator");
el.setAttribute("ID", "your-new-id");

Upvotes: 1

Mr. Roshan
Mr. Roshan

Reputation: 1805

Try This:-

document.getElementById("Numerator").value = "My value";

Upvotes: 1

Related Questions