Home > Article > Web Front-end > How to change input value in javascript
Change method: 1. Use the value attribute, the syntax "input object.value = "modified value";"; 2. Use the setAttribute() method, the syntax "input object.setAttribute("value", "Modified value");".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to change the input value in javascript
Method 1: Use the value attribute
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <input id="inp" value="默认值" /> <br /><br /> <button onclick="myFunction()">修改input的值</button> <script> function myFunction() { var input = document.getElementById("inp"); input.value = "修改后的值"; } </script> </body> </html>
Method 2: Using the setAttribute() method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <input id="inp" value="默认值" /> <br /><br /> <button onclick="myFunction()">修改input的值</button> <script> function myFunction() { var input = document.getElementById("inp"); input.setAttribute("value","修改后的值"); } </script> </body> </html>
[Related recommendations: javascript Study tutorial】
The above is the detailed content of How to change input value in javascript. For more information, please follow other related articles on the PHP Chinese website!