This time I will show you how to use JS to obtain and modify the input text box data, and what are the precautions for using JS to obtain and modify the input text box data. The following is a practical case. Let’s take a look. .
1 Introduction
Getting the text box and modifying its content can be achieved using thegetElementById() method.
getElementById()The method can get the HTML tag by the specified id and return it.
Syntax:
##sElement=document.getElementById(id)<a href="http://www.php.cn/code/658.html" target="_blank"></a>
: Used to receive an object returned by this method.
id: Used to set the id value that needs to be obtained from the HTML tag.
Second ApplicationGet the text box and modify its content
It will be displayed in the text box after the page is loaded "Initial text content", the content in the text box will be changed when the button is clicked.
Three Code<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>获取文本框并修改其内容</title>
</head>
<body>
<script language="javascript">
<!--
function c1()
{
var t=document.getElementById("txt");
t.value="www.jb51.net 修改文本内容"
}
-->
</script>
<input type="text" id="txt" value="初始文本内容" size="30"/>
<input type="button" value="更改文本内容" name="btn" onclick="c1();" />
</body>
</html>
Recommended reading:
How to use React high-order componentsHow to correctly solve cross-domain problems in Vue projectsThe above is the detailed content of How to get and modify input text box data using JS. For more information, please follow other related articles on the PHP Chinese website!