Home > Article > Web Front-end > How to replace the selected value of a text box with javascript
In JavaScript, you can use the value attribute to replace the value selected in the text box. This attribute can set or return the default value of the password field. You can modify the text box by obtaining the text box object and modifying its value attribute. The value, the syntax is "specify text selection object.value='replaced value'".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
How to replace the value selected in the text box with javascript
In js, by obtaining the text box object and modifying its value attribute value, you can Modify the value of the text box.
The example is as follows:
Use the input tag to create a text box and set its id attribute to test, which is used to obtain the text box object below. Set the default value of the input text box through value. For example, the default value set here is 5566.
Use the button tag to create a button, bind the onclick event to the button button, and when the button is clicked, execute the editinput() function.
In the js tag, create the editinput() function. Within the function, obtain the input object through the id, and modify the value of the text box by reassigning the value attribute of the object.
The syntax is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <input type="text" value="5566" id="test"> <button onclick="editinput()">修改</button> <script> function editinput() { document.getElementById("test").value='8888'; } </script> </body> </html>
Output result:
【Related recommendations:
The above is the detailed content of How to replace the selected value of a text box with javascript. For more information, please follow other related articles on the PHP Chinese website!