テキストフィールドの内容を読み出す

説明

テキストフィールドの内容を読み出すにはinputタグのvalueプロパティを読み出します。

サンプルプログラム

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<link rel="stylesheet" href="css/main.css" type="text/css" media="all">
<script type="text/javascript" src="js/sample.js"></script>
</head>
<body>
<h1>テキストフィールドの内容を読み出す</h1>
<form method="get" action="./regist.cgi" id="aForm">
名前:<input type="text" name="userName" id="userName" value="名無太郎">
<input type="button" value="内容を読み出す" id="checkButton">
</form>
</body>
</html>

【スクリプト】
window.onload = function(){
document.getElementById("checkButton").onclick = function(){
var txt = document.getElementById("userName").value;
alert(txt);
}
}
サンプルを実行
[戻る]