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

How to set the value of the input value attribute in javascript

青灯夜游
Release: 2023-01-07 11:44:27
Original
14122 people have browsed it

In javascript, you can use the setAttribute() method to set the input value. This method can set the attribute value of the specified element; the syntax format is "input element object.setAttribute("value", "value")" .

How to set the value of the input value attribute in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the setAttribute() method to set the value of the input value attribute.

The setAttribute() method of the element can set the attribute value of the element. The usage is as follows:

setAttribute(name, value)
Copy after login

The parameters name and value represent the attribute name and attribute value respectively. Property names and property values ​​must be passed as strings. If the specified attribute exists in the element, its value will be refreshed; if it does not exist, the setAttribute() method will create the attribute for the element and assign a value.

Example: Set input value

<input id="input" type="text" value="hello" />
<script>
	var input = document.getElementById("input");  //获取input对象
	input.setAttribute("value", "你好");  //为input对象设置value属性和值
</script>
Copy after login

Rendering:

How to set the value of the input value attribute in javascript

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to set the value of the input value attribute 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