Thuộc tính defaultValue của <input type="month"> trả về hoặc thiết lập **giá trị mặc định** (giá trị ban đầu khi trang tải, trước khi người dùng thay đổi). Định dạng: YYYY-MM (VD: 2019-10).
Cú pháp
Lấy giá trị:
monthInput.defaultValue
Thiết lập giá trị:
monthInput.defaultValue = "YYYY-MM"
Ví dụ: Thay đổi giá trị mặc định và áp dụng
<!DOCTYPE html>
<html>
<head>
<style>
html, body { height: 100%; margin: 0; font-family: sans-serif; }
body {
text-align: center; color: white;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
}
input[type="month"] {
width: 35%; border: 2px solid white; background: transparent;
color: white; font-weight: bold; padding: 8px; margin: 10px auto; display: block;
}
.btn {
background: #0197F6; border: none; color: white;
padding: 10px 20px; border-radius: 4px; width: 35%;
margin: 20px auto; display: block; cursor: pointer;
}
.show { font-size: 1.5rem; font-weight: bold; margin-top: 15px; }
</style>
</head>
<body>
<h1>Ví dụ Input Month defaultValue</h1>
<p>Nhập tháng sinh của bạn</p>
<input type="month" class="monthInput">
<button onclick="changeDefaultValue()" class="btn">Gửi (sẽ bị ghi đè)</button>
<div id="result" class="show"></div>
<script>
function changeDefaultValue() {
var input = document.querySelector(".monthInput");
var msg = document.getElementById("result");
input.defaultValue = "1997-10"; // Thiết lập defaultValue mới
input.value = input.defaultValue; // Áp dụng ngay
msg.textContent = ":D Haha! Tháng sinh của bạn giờ là Tháng 10/1997!";
}
</script>
</body>
</html>
Kết quả
Chọn tháng bất kỳ (VD: 2020-05):

Nhấn "Gửi" - giá trị bị ghi đè thành 1997-10:


🔑 Phân biệt:
value: Giá trị **hiện tại** (người dùng có thể thay đổi).defaultValue: Giá trị **ban đầu** (từ HTML hoặc set bằng JS).- Khi reset form (
<input type="reset">),valuequay vềdefaultValue.