I have the following input:
<input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>
How can I use jQuery to make this element a read-only input without changing the element or its value?
I have the following input:
<input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>
How can I use jQuery to make this element a read-only input without changing the element or its value?
simply add the following attribute
// for disabled i.e. cannot highlight value or change
disabled="disabled"
// for readonly i.e. can highlight value but not change
readonly="readonly"
jQuery to make the change to the element (substitute disabled
for readonly
in the following for setting readonly
attribute).
$('#fieldName').attr("disabled","disabled")
or
$('#fieldName').attr("disabled", true)
Maybe use atribute disabled:
<input disabled="disabled" id="fieldName" name="fieldName" type="text" class="text_box" />
Or just use label tag: ;)
<label>
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<div>
<input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname" />
</div>
</body>
<script type="text/javascript">
$(function()
{
$('#fieldName').attr('disabled', 'disabled');
});
</script>
</html>