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

How to set value attribute in javascript

青灯夜游
青灯夜游Original
2021-04-12 17:17:3311538browse

How to set the value attribute in JavaScript: First use the document.getElementById() method to obtain the specified DOM object; then set it with the "DOM object.value="value";" statement.

How to set value attribute in javascript

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

javascript setting value

Some elements have value attributes, such as input, bottom, select, etc. You can use the value attribute to modify its value.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>修改value</title>
    </head>
    <body>

        <input type="text" name="hello" value="Hello krray" id="hello">
        <script>
            var a=document.getElementById("hello");
            alert( a.value);
            a.value="你好,小凯"
        </script>
    </body>
</html>

HTML DOM value attribute

The value attribute is used to set or return the value of the attribute.

Syntax:

  • Set attribute value

attribute.value=value
  • Return attribute value

attribute.value

Return value: string, representing the value of the attribute.

【Recommended learning: javascript advanced tutorial

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