Reputation: 85
Is it possible to change input field using javascript onclick?? My code:
<form>
<input type="text" id="demo" value="John">
</form>
<button onclick="myFunction()">Click me!</button>
<script>
function myFunction() {
document.getElementById("demo").value = "Hello World";
}
</script>
it is successful to change the text, but it is failed to change input field value
anyidea how to solve it??? Thank you very much
Upvotes: 1
Views: 2626
Reputation: 8751
To update the value
field of the HTML as well, use setAttribute()
<form>
<input type="text" id="demo" value="John">
</form>
<button onclick="myFunction()">Click me!</button>
<script>
function myFunction() {
document.getElementById("demo").setAttribute("value", "Hello World");
}
</script>
Upvotes: 1