คุณสมบัติประเภทรหัสผ่าน HTML DOM Input เชื่อมโยงกับองค์ประกอบอินพุตที่มี type=”password” มันจะส่งคืนรหัสผ่านสำหรับองค์ประกอบรหัสผ่านที่ป้อนเสมอ
ไวยากรณ์
ต่อไปนี้เป็นรูปแบบไวยากรณ์สำหรับคุณสมบัติประเภทรหัสผ่าน -
passwordObject.type
ตัวอย่าง
ให้เราดูตัวอย่างคุณสมบัติประเภทรหัสผ่านอินพุต -
<!DOCTYPE html>
<html>
<body>
<h1>password type property</h1>
PASSWORD: <input type="password" id="PASS1">
<p>Get the above element type by clicking the below button</p>
<button onclick="getType()">Get Type</button>
<p id="Sample"></p>
<script>
function getType() {
var t = document.getElementById("PASS1").type;
document.getElementById("Sample").innerHTML = "The type for the input field is : "+t;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม "รับประเภท" -

ในตัวอย่างข้างต้น −
เราได้สร้างช่องป้อนข้อมูลด้วยรหัสผ่านประเภทและตั้งค่ารหัสเป็น “PASS1”
PASSWORD: <input type="password" id="PASS1">
เราได้สร้างปุ่ม "Get Type" ที่จะรันเมธอด getType() เมื่อผู้ใช้คลิก -
<button onclick="getType()">Get Type</button>
เมธอด getType() รับองค์ประกอบอินพุตโดยใช้เมธอด getElementById() และกำหนดค่าแอตทริบิวต์ type ให้กับตัวแปร t ตัวแปรนี้จะแสดงในย่อหน้าด้วย id “Sample” โดยใช้คุณสมบัติ innerHTML -
function getType() {
var t = document.getElementById("PASS1").type;
document.getElementById("Sample").innerHTML = "The type for the input field is : "+t;
}