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.


document.getElementById("").readOnly = true

document.getElementById('TextBoxID').readOnly = true;    //to enable readonly


document.getElementById('TextBoxID').readOnly = false;   //to  disable readonly

The above answers did not work for me. The below does:

document.getElementById('input_field_id').setAttribute('readonly', true);

And to remove the readonly attribute:

document.getElementById('input_field_id').removeAttribute('readonly');

And for running when the page is loaded, it is worth referring to here.