We help IT Professionals succeed at work.

how to read textbox attribute through jquery and findout is it readonly

hariaspind
hariaspind asked
on
How to get the value of texbox attribute readonly and set other textbox to readyonly
Comment
Watch Question

Commented:
The attr function: http://api.jquery.com/attr/

Author

Commented:
I tried this  .this code sets the  readonly att to "true" .but the textbox is still editable.But why ?

$("#divResult").find("textarea").attr('ReadOnly', true);

Commented:
true isn't a boolean, but text ;). Try:
$("#divResult").find("textarea").attr('ReadOnly', 'true'); - works for me
leakim971Multitechnician
Distinguished Expert 2019

Commented:
check this :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("#but").click(function() {
			// get the value of the readonly textbox
			var val = $("input[readonly='readonly']").val();
			// set the value of the writeable textbox
			$("input[readonly!='readonly'][type='text']").val(val);
			// set it as readonly
			$("input[readonly!='readonly'][type='text']").attr("readonly","readonly");
		});
	});
</script>
</head>
<body>
<input type="text" readonly="readonly" value="I'm readonly" />
<br />
<br />
<input type="text" value="you can modify my value" />
<br />
<br />
<input type="button" id="but" value="search readony and set readonly" />
</body>
</html>

Open in new window

Multitechnician
Distinguished Expert 2019
Commented:
One shot : $("input[readonly!='readonly'][type='text']").val($("input[readonly='readonly']").val()).attr("readonly","readonly")


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("#but").click(function() {
			// get the value of the readonly textbox
			//var val = $("input[readonly='readonly']").val();
			// set the value of the writeable textbox
			//$("input[readonly!='readonly'][type='text']").val(val);
			// set it as readonly
			//$("input[readonly!='readonly'][type='text']").attr("readonly","readonly");
			// Do it with one shot !
			$("input[readonly!='readonly'][type='text']").val($("input[readonly='readonly']").val()).attr("readonly","readonly");
		});
	});
</script>
</head>
<body>
<input type="text" readonly="readonly" value="I'm readonly" />
<br />
<br />
<input type="text" value="you can modify my value" />
<br />
<br />
<input type="button" id="but" value="search readony and set readonly" />
</body>
</html>

Open in new window

Author

Commented:
Thank.I got my issue fixed.