Home  >  Article  >  Web Front-end  >  How to set the value of the input value attribute in javascript

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

青灯夜游
青灯夜游Original
2021-07-20 15:46:5614135browse

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)

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>

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!

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