Phương thức reset() của Form trong HTML DOM dùng để reset tất cả giá trị của các phần tử form về giá trị mặc định được chỉ định bởi thuộc tính value. Nó đóng vai trò như nút reset để xóa dữ liệu form và không nhận tham số.
Cú pháp
Cú pháp cho phương thức reset():
formObject.reset()
Ví dụ
Ví dụ minh họa cho phương thức reset():
<!DOCTYPE html> <html> <head>
<style>
form{ border:2px solid blue; margin:2px; padding:4px; }
</style>
<script>
function ResetForm() {
document.getElementById("FORM1").reset();
document.getElementById("Sample").innerHTML = "Form đã được reset";
}
</script> </head> <body>
<h1>Ví dụ phương thức reset() của Form</h1>
<form id="FORM1" method="post" action="/sample_page.php">
<label>Tên người dùng <input type="text" name="usrN"></label> <br><br>
<label>Tuổi <input type="text" name="Age"></label> <br><br>
<input type="submit" value="GỬI">
<input type="button" onclick="ResetForm()" value="RESET">
</form>
<p id="Sample"></p>
</body> </html>
Kết quả
Khi nhấn nút "RESET", tất cả trường form xóa trống và về giá trị mặc định.
Giải thích ví dụ
Tạo form với hai trường text, nút submit và nút button type="button" gọi ResetForm():
<input type="button" onclick="ResetForm()" value="RESET">
Hàm ResetForm() lấy form và gọi reset(), sau đó hiển thị thông báo:
function ResetForm() {
document.getElementById("FORM1").reset();
document.getElementById("Sample").innerHTML = "Form đã được reset";
}