Home > Article > Web Front-end > How to set the value of the input value attribute in javascript
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")" .
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:
[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!