Home > Web Front-end > JS Tutorial > body text

How to change input value in javascript

青灯夜游
Release: 2022-01-26 14:36:40
Original
14049 people have browsed it

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");".

How to change input value in javascript

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>
Copy after login

How to change input value in javascript

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>
Copy after login

How to change input value in javascript

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!