50

I know you can add readonly="readonly" to an input field so its not editable. But I need to use javascript to target the id of the input and make it readonly as I do not have access to the form code (it's generated via marketing software)

I don't want to disable the input as the data should be collected on submit.

Here is the page I have added in the below suggestion with no luck so far:

https://www.pages05.net/engagedigital/inputReadOnly/test?spMailingID=6608614&spUserID=MTI5MDk4NjkzMTMS1&spJobID=Nzk4MTY3MDMS1&spReportId=Nzk4MTY3MDMS1

Make sure you use <body onload="onLoadBody();"> for anyone using this in the future.

Cœur
  • 32,421
  • 21
  • 173
  • 232
chris
  • 691
  • 1
  • 6
  • 15

7 Answers7

85

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.

nbro
  • 12,226
  • 19
  • 85
  • 163
Vijay
  • 7,211
  • 9
  • 38
  • 69
  • I appreciate the response, but its doesnt seem to be working for me - do you mind checking: http://bit.ly/1aI1Zxn ??? – chris Jul 24 '13 at 07:57
  • 2
    Awesome - thanks Vijay, i just had to make the body reference have - just in case anyone sees this in the future. – chris Jul 25 '13 at 02:36
  • I've added a note on the case sensitivity of the property in JS. – insaner Jun 11 '16 at 19:53
  • I have one more question. If radio is hidden, in that case eventhough its true, value comes as false. – Altaf Patel Mar 16 '18 at 13:41
  • 1
    Note the difference, in HTML the attribute is `readonly` (all lower case) and in Javascript it is `readOnly` (with capital O). Thanks for the answer. – Simon Jul 02 '20 at 13:46
29

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.

Community
  • 1
  • 1
Charlie Haley
  • 3,262
  • 3
  • 20
  • 34
6
document.getElementById('TextBoxID').readOnly = true;    //to enable readonly


document.getElementById('TextBoxID').readOnly = false;   //to  disable readonly
Mumthezir VP
  • 5,359
  • 5
  • 24
  • 54
4
document.getElementById("").readOnly = true
gezzuzz
  • 178
  • 2
  • 15
4

Try This :

document.getElementById(<element_ID>).readOnly=true;
Sandiip Patil
  • 412
  • 1
  • 4
  • 21
-3

I think you just have readonly="readonly"

<html><body><form><input type="password" placeholder="password" valid="123" readonly=" readonly"></input>

Liam
  • 1
-4

Here you have example how to set the readonly attribute:

<form action="demo_form.asp">
  Country: <input type="text" name="country" value="Norway" readonly><br>
  <input type="submit" value="Submit">
</form>
bobofrut
  • 3
  • 2