1

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 muchenter image description here

1 Answer 1

1

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>

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

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.