Thuộc tính giá trị ẩn đầu vào HTML DOM trả về và sửa đổi nội dung của thuộc tính giá trị của trường nhập loại =”hidden” trong tài liệu HTML.
Cú pháp
Sau đây là cú pháp -
1. Giá trị trả về
object.value
2. Sửa đổi giá trị
object.value=”text”
Ví dụ
Hãy để chúng tôi xem một ví dụ về thuộc tính giá trị ẩn đầu vào HTML DOM -
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
background-color:#F19A3E;
color:#fff;
}
.btn{
background-color:#3C787E;
border:none;
height:2rem;
border-radius:50px;
width:60%;
margin:1rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
.show{
color:#fff;
font-size:1.2rem;
font-weight:600;
}
</style>
</head>
<body>
<h1>DOM Hidden value Property Example</h1>
Here is hidden input field:
<input type="hidden" class="input-field" value="Hi! I was previous value of hidden input">
<button onclick="getType()" class="btn">Click to change value</button>
<div class="show"></div>
<script>
function getType() {
var inputField = document.querySelector(".input-field");
var msgDiv = document.querySelector(".show");
msgDiv.innerHTML = "" + inputField.value + "";
inputField.value = "Hello! I'm new value of hidden input"
msgDiv.innerHTML +="" + inputField.value + "";
}
</script>
</body>
</html> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Bảng điều khiển -
Nhấp vào “ Nhấp để thay đổi giá trị ”Để thay đổi giá trị của trường nhập ẩn. Xem ảnh chụp màn hình bảng điều khiển để dễ hiểu -
Bảng điều khiển -