Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM Input Password value


คุณสมบัติ value ของ HTML DOM Input password เกี่ยวข้องกับ input element ที่มี type="password" และมีแอตทริบิวต์ value ใช้สำหรับระบุค่าเริ่มต้น (default value) สำหรับฟิลด์รหัสผ่าน และเปลี่ยนค่าเป็นอินพุตของผู้ใช้

ไวยากรณ์ (Syntax)

passwordObject.value = text;

ที่นี่ text ใช้ระบุค่าสำหรับฟิลด์รหัสผ่าน

ตัวอย่าง (Example)

<!DOCTYPE html> <html> <body> 
<h1>Input password Value property</h1> 
PASSWORD: <input type="password" id="PASS1"> 
<p>Get the above element value by clicking the below button</p> 
<button onclick="getValue()">Get Value</button> 
<p id="Sample"></p> 
<script> 
  function getValue() { 
    var t = document.getElementById("PASS1").value; 
    document.getElementById("Sample").innerHTML = "The value for the input field is : "+t; 
  } 
</script> </body> </html>

ผลลัพธ์ (Output)

ผลลัพธ์ที่ได้:

คุณสมบัติ HTML DOM Input Password value

เมื่อพิมพ์ข้อความในฟิลด์รหัสผ่านและคลิก "Get Value":

คุณสมบัติ HTML DOM Input Password value

คำอธิบายโค้ด

สร้าง input type password id="PASS1":

PHONE NO: <input type="number" value="222" id="NUMBER1">

สร้างปุ่ม "Get Value" ทำงาน getValue() เมื่อคลิก:

<button onclick="getValue()">Get Value</button>

getValue() ดึง input element ด้วย getElementById() นำค่าแอตทริบิวต์ "value" มาเก็บในตัวแปร t จะคืนค่าสตริงว่างหากไม่ได้พิมพ์อะไรก่อนคลิก value เปลี่ยนตามอินพุตผู้ใช้ จะแสดงค่าปัจจุบันในฟิลด์รหัสผ่าน แสดงตัวแปรในพารากราฟ id "Sample" ด้วย innerHTML:

function getValue() { 
  var t = document.getElementById("PASS1").value; 
  document.getElementById("Sample").innerHTML = "The value for the input field is : "+t; 
}