คุณสมบัติ readOnly ของ Textarea ใน HTML DOM ใช้สำหรับอ่านหรือตั้งค่าว่าเนื้อหาของ textarea ควรเป็นแบบอ่านอย่างเดียว (read-only) หรือไม่
รูปแบบการใช้งาน (Syntax)
1. อ่านค่า readOnly
object.readOnly
2. ตั้งค่า readOnly
object.readOnly = true | false
ตัวอย่างการใช้งาน
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
background: lightseagreen;
height: 100vh;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
<body>
<h1>ตัวอย่างคุณสมบัติ readOnly ของ Textarea</h1>
<textarea rows="5" cols='20' name="I'm name attribute of textarea element"> Hi! I'm a text area element with some dummy text. </textarea>
<button onclick="set()" class="btn">สลับสถานะ Read Only</button>
<script>
function set() {
var ta = document.querySelector("textarea");
if (ta.readOnly === true) {
ta.readOnly = false;
} else {
ta.readOnly = true;
}
}
</script>
</body>
</html>
ผลลัพธ์
คลิกที่ปุ่ม "สลับสถานะ Read Only" เพื่อสลับค่าของแอตทริบิวต์ readOnly ของ textarea

