Home >Web Front-end >Front-end Q&A >How to replace the selected value of a text box with javascript

How to replace the selected value of a text box with javascript

WBOY
WBOYOriginal
2022-01-12 16:09:292867browse

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'".

How to replace the selected value of a text box with javascript

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=&#39;8888&#39;;
    }
  </script>
</body>
</html>

Output result:

How to replace the selected value of a text box with javascript

##After clicking the button:

How to replace the selected value of a text box with javascript

【Related recommendations:

javascript learning tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is API in JavaScriptNext article:What is API in JavaScript