How to add a “readonly” attribute to an input?
jQuery <1.9
$('#inputId').attr('readonly', true);
jQuery 1.9+$('#inputId').prop('readonly', true);
Read more about difference between prop and attr How to add the readonly attribute with JQuery or Javascript (BUT following the W3C standard!)
The proper way to do it is with :
$('#someid').prop('readonly', true);
or$('#someid').prop('readonly', false);
and $('#someid').removeProp('readonly');
works fine as well as these are all jQuery methods for the native:document.getElementById('someid').readOnly = true;
which sets the readonly
property appropriately, and if you inspect the element in the console you'll see that the readonly
attribute is added without a value like it should be according to the W3C specifications.readonly
is a property, and prop()
is the method for setting properties.
The specifications for HTML5 says:
this means the following is valid:readonly = "readonly" or "" (empty string) or empty
Specifies that element represents a control whose value is not meant to be edited.
<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
How can i add readonly attribute on all input fields using javascript?
You can use element selector with attribute value selector to select all the textboxes and apply the readonly
property to all of the selected elements.
$('input[type="text"]').prop('readonly', true);
To select all the textboxes in the form$('yourFormSelector input[type="text"]').prop('readonly', true);
As an alternative to attribute value selector, you can also use :text
selector to select all textboxes.$(':text').prop('readonly', true);
How to make a input field readonly with JavaScript?
You can get the input element and then set its readOnly
property to true
as follows:
document.getElementById('InputFieldID').readOnly = true;
Specifically, this is what you want:<script type="text/javascript">
function onLoadBody() {
document.getElementById('control_EMAIL').readOnly = true;
}
</script>
Call this onLoadBody()
function on body tag like:<body onload="onLoadBody">
View Demo: jsfiddle. Make all inputs with class readOnly
you can do this using this
jQuery <1.9
<script>
$(function () {
$('input[type="text"], textarea').attr('readonly','readonly');
});
</script>
or use this for readonly modejQuery 1.9+
$(function () {
$('input[type="text"], textarea').prop("readonly",true);
});
and remove readonly modejQuery <1.9
$(function () {
$('input[type="text"], textarea').removeAttr('readonly');
});
</script>
or use this for removejQuery 1.9+
$(function () {
$('input[type="text"], textarea').prop("readonly",false);
});
How to dynamically add ng-readonly attribute
You can make use of: Compiler (aka $complie) is an AngularJS service which traverses the DOM looking for attributes. https://docs.angularjs.org/guide/compiler
Something like this should work:
var template = '<input name="clientNames" type="text" class="form-control" ng-readonly="setReadonly" />';
var newClientNameInputBox = angular.element(template);
var $compile = ...; // injected into your code
var scope = ...; // { setReadonly : true }
var parent = ...; // DOM Parent where you want to drop your input
var compiledElement = $compile(newClientNameInputBox)(scope);
parent.appendChild(compiledElement);
Adding readonly attribute to all form elements
Try this:
$('#form1 input').attr('readonly', 'readonly');
- You may want to include more elements
#form1 input, #form1 textarea, #form1 select
- In jQuery, you usually don't need to iterate over the collection.
attr
would work for a collection same as for a single element. - In your case,
#form1
matched just the<form>
element, andeach
was triggered once, for that element. To find all elements (input or not), you can write#form1 *
.
Related Topics
How to Change the Pop-Up Position of the Jquery Datepicker Control
Cross-Browser Bookmark/Add to Favorites JavaScript
Using the Haversine Formula in JavaScript
How to Bind to the Change Event of a Textarea in Jquery
Differencebetween Settimeout(Fn, 0) and Settimeout(Fn, 1)
Is Localstorage.Getitem('Item') Better Than Localstorage.Item or Localstorage['Item']
Serialize Object to Query String in JavaScript/Jquery
JavaScript Loop Variable Scope
Why Threre Is No Way to Download File Using Ajax Request
Google Maps API Autocomplete 2Nd Address Fields on Same Page
How to Invoke "Click" Event Programmatically in D3
How to Show Current Time in JavaScript in the Format Hh:Mm:Ss
How to Access Nested JSON Data
Why Escape_JavaScript Before Rendering a Partial
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
Professional Jquery Based Combobox Control
JavaScript - Generating All Combinations of Elements in a Single Array (In Pairs)