0

How to set the value of an input field in JavaScript?

var button = document.querySelector('button');

button.addEventListener('click', () => {
  document.getElementById("mytext").value = "My value";
});
<input type="text" id="mytext" placeholder="Empty..."/>

<button>
  Enter value
</button>
input {
  padding: 10px;
  font-size: 24px;
}

button {
  padding: 10px;
  font-size: 24px;
}
RUN
add comment
Suggest a different solution or add another example