Thuộc tính name của <input type="hidden"> trả về hoặc thiết lập tên trường form. Tên này dùng làm key khi submit (VD: $_POST['ten']).
Cú pháp
Lấy giá trị:
hiddenInput.name
Thiết lập giá trị:
hiddenInput.name = "ten_moi"
Ví dụ: Lấy name của hidden input
<!DOCTYPE html>
<html>
<head>
<style>
body { text-align: center; background: #F19A3E; color: white; font-family: sans-serif; padding: 20px; }
.btn {
background-color: #3C787E; border: none; color: white;
padding: 10px 20px; border-radius: 25px; width: 60%;
margin: 15px auto; display: block; cursor: pointer;
}
.show { color: white; font-size: 1.2rem; font-weight: 600; margin-top: 15px; }
</style>
</head>
<body>
<h1>Ví dụ Hidden name Property</h1>
Nhập tên (ẩn): <input type="hidden" class="input-field" name="Hi_Im_hidden_field">
<button onclick="getName()" class="btn">Lấy name của hidden input</button>
<div id="result" class="show"></div>
<script>
function getName() {
var input = document.querySelector(".input-field");
document.getElementById("result").textContent = input.name;
}
</script>
</body>
</html>
Kết quả
Trước khi nhấn:

Sau khi nhấn nút:

⚠️ Lưu ý: Giống như file input, thay đổi
name bằng JS sau khi render **không ảnh hưởng** đến form submit thông thường. Chỉ có tác dụng khi build form hoàn toàn bằng JS.