In jQuery how do you make a textbox read only and how do you turn it back.
This is what I tried:
$("#<%= TextBox1.ClientID %>").readonly();
In jQuery how do you make a textbox read only and how do you turn it back.
This is what I tried:
$("#<%= TextBox1.ClientID %>").readonly();
Hi there,
You set the readonly
attribute to true to make the input field read only.
You can use removeAttr
to remove it, or set it to false.
Here’s a small example:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Read only textbox example</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<input type="text" id="myTextBox" value="Well, hello there!" />
</div>
<button id="myButton">Make read-only</button>
<script>
$(document).ready(function() {
var t = $("#myTextBox");
var b = $("#myButton");
b.on("click", function(){
if (t.attr('readonly')){
t.removeAttr('readonly');
b.text("Make read-only");
} else {
t.attr('readonly', true);
b.text("Remove read-only");
}
});
});
</script>
</body>
</html>
Thanx exactly what i needed.