HTML DOM の input month 型(type)プロパティの使い方を解説
HTML DOM の input month 型(type)プロパティは、HTML ドキュメント内にある「月」入力フィールド(<input type="month">)の type 属性の値を取得するために使用されます。このプロパティを読み取ることで、対象の入力要素がどの種類(type)であるかをプログラムから確認できます。
構文
type プロパティの基本的な構文は以下のとおりです。
object.type
サンプルコード
ここでは、HTML DOM の input month 型プロパティを使った具体的な例を見ていきましょう。ボタンをクリックすると、画面上に入力フィールドの type 値が表示される仕組みです。
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%)
center/cover no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.1rem;
}
input{
display:block;
width:35%;
border:2px solid #fff;
background-color:transparent;
color:#fff;
font-weight:bold;
padding:8px;
margin:1rem auto;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
color:#db133a;
font-weight:bold;
}
</style>
</head>
<body>
<h1>DOM Input month type プロパティのデモ</h1>
<p>こんにちは!私の type 値を知りたくありませんか?</p>
<input type="month" class="monthInput" required>
<button type="button" onclick="getType()" class="btn">クリックして type 値を取得</button>
<div class="show"></div>
<script>
function getType() {
var monthInput = document.querySelector(".monthInput");
document.querySelector(".show").innerHTML="type = " + monthInput.type;
}
</script>
</body>
</html>実行結果
上記のコードを実行すると、ブラウザには次のような画面が表示されます。

表示された「クリックして type 値を取得」ボタンを押すと、JavaScript の getType() 関数が呼び出され、月入力フィールドの type 属性の値が画面に表示されます。

解説のポイント
document.querySelector()を使って、クラス名がmonthInputの入力要素を取得しています。monthInput.typeにアクセスすることで、その要素の type 属性値(この場合はmonth)を文字列として取得できます。- 取得した値は
innerHTMLを使って、結果表示用の div 要素に出力しています。
このように type プロパティを活用すれば、JavaScript からフォーム要素の種類を動的に判定することが可能になり、バリデーション処理や条件分岐など、さまざまな場面で役立ちます。
-
HTML DOM Input Time の type プロパティとは?使い方とサンプルコードを解説
HTML DOM の Input Time type プロパティは、<input type=time> 要素の type 属性の値を取得または設定するために使用されます。このプロパティを使うことで、JavaScript から動的に入力フィールドの種類を確認したり変更したりすることができます。 構文 type プロパティの基本的な構文は以下の通りです。 type の値を取得する場合 inputTimeObject.type type に値を設定する場合 inputTimeObject.type = stringValue 指定できる文字列値(stringValue) stri
-
HTML DOM Input Week の type プロパティとは?使い方を解説
はじめにHTML DOM の Input Week type プロパティは、週(week)型入力フィールドの type 属性を取得・設定するためのプロパティです。このプロパティを使うことで、JavaScript から対象の入力要素がどの種類(type)であるかを確認したり、別の type 値に変更したりすることができます。 構文 type プロパティの基本的な構文は以下の通りです。 値を取得する場合 inputWeekObject.type 要素の type 属性の現在値が文字列として返されます。 値を設定する場合 inputWeekObject.type = stringValue 指定した