Thuộc tính form của nút <input type="button|submit|reset"> trả về tham chiếu đến phần tử <form> chứa nút đó. Thuộc tính này **chỉ đọc**.
Cú pháp
buttonElement.form
Ví dụ: Xác định form chứa nút
<!DOCTYPE html>
<html>
<head>
<style>
body { text-align: center; font-family: sans-serif; padding: 20px; }
form { margin: 20px auto; border: 1px solid #ddd; padding: 20px; max-width: 400px; }
fieldset { border: 1px solid #ccc; border-radius: 5px; margin-bottom: 15px; }
legend { font-weight: bold; color: #333; }
.btn {
display: block; margin: 10px auto;
background-color: #dc3545; color: white; border: 1px solid #dc3545;
padding: 10px; border-radius: 25px; width: 80%;
font-weight: bold; cursor: pointer;
}
.msg { font-weight: bold; font-size: 1.2rem; color: #ffc107; margin-top: 15px; }
</style>
</head>
<body>
<h1>Ví dụ form Property</h1>
<form id="form1">
<fieldset>
<legend>Form 1</legend>
<input type="button" class="btn" value="Nút 1 - Form 1">
<input type="button" class="btn" value="Nút 2 - Form 1">
</fieldset>
</form>
<form id="form2">
<fieldset>
<legend>Form 2</legend>
<input type="button" class="btn" value="Nút 1 - Form 2">
<input type="button" class="btn" value="Nút 2 - Form 2">
</fieldset>
</form>
<div id="msg" class="msg"></div>
<script>
var buttons = document.querySelectorAll(".btn");
var msg = document.getElementById("msg");
buttons.forEach(function(btn) {
btn.addEventListener("click", function(e) {
msg.textContent = "";
if (e.target.form.id === 'form1') {
msg.textContent = "Tôi thuộc Form 1";
} else {
msg.textContent = "Tôi thuộc Form 2";
}
});
});
</script>
</body>
</html>
Kết quả
Hai form với các nút:

Nhấn nút trong Form 1:

Nhấn nút trong Form 2:

💡 Ứng dụng: Rất hữu ích khi xử lý nhiều form trên cùng trang, ví dụ: xác định form nào đang submit để validate dữ liệu riêng biệt.