I have the following input:
How c
Use this example to make text box ReadOnly or Not.
<input type="textbox" class="txt" id="txt"/>
<input type="button" class="Btn_readOnly" value="Readonly" />
<input type="button" class="Btn_notreadOnly" value="Not Readonly" />
<script>
$(document).ready(function(){
('.Btn_readOnly').click(function(){
$("#txt").prop("readonly", true);
});
('.Btn_notreadOnly').click(function(){
$("#txt").prop("readonly", false);
});
});
</script>
In html
$('#raisepay_id').attr("readonly", true)
$("#raisepay_id").prop("readonly",true);
in bootstrap
$('#raisepay_id').attr("disabled", true)
$("#raisepay_id").prop("disabled",true);
JQuery is a changing library and sometimes they make regular improvements. .attr() is used to get attributes from the HTML tags, and while it is perfectly functional .prop() was added later to be more semantic and it works better with value-less attributes like 'checked' and 'selected'.
It is advised that if you are using a later version of JQuery you should use .prop() whenever possible.
In JQuery 1.12.1, my application uses code:
$('"#raisepay_id"')[0].readOnly=true;
$('"#raisepay_id"')[0].readOnly=false;
and it works.
The setReadOnly(state) is very useful for forms, we can set any field to setReadOnly(state) directly or from various condition.But I prefer to use readOnly for setting opacity to the selector otherwise the attr='disabled' also worked like the same way.
readOnly examples:
$('input').setReadOnly(true);
or through the various codition like
var same = this.checked;
$('input').setReadOnly(same);
here we are using the state boolean value to set and remove readonly attribute from the input depending on a checkbox click.
To make an input readonly use:
$("#fieldName").attr('readonly','readonly');
to make it read/write use:
$("#fieldName").removeAttr('readonly');
adding the disabled
attribute won't send the value with post.
Perhaps it's meaningful to also add that
$('#fieldName').prop('readonly',false);
can be used as a toggle option..