How to make a input field readonly with JavaScript?
Asked Answered
C

9

75

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.

Convulsant answered 24/7, 2013 at 4:59 Comment(1)
element.setAttribute("readonly", "readonly");? jsfiddle.net/DerekL/e8yCdProstration
G
123

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.

Groscr answered 24/7, 2013 at 5:22 Comment(4)
I appreciate the response, but its doesnt seem to be working for me - do you mind checking: bit.ly/1aI1Zxn ???Convulsant
Awesome - thanks Vijay, i just had to make the body reference have <body onload="onLoadBody();"> - just in case anyone sees this in the future.Convulsant
I've added a note on the case sensitivity of the property in JS.Phytopathology
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.Direction
H
48

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.

Haphazard answered 6/7, 2016 at 18:39 Comment(2)
Note that setAttribute('readonly', false) does not work -- it will still be true! You have to remove it.Alleviation
@Alleviation that is to be expected - HTML boolean attributes are based on their presence not on their value. Common mistake when people do checked="false" etc... HTML attribute is about presence while DOM object property is about value.Abdication
A
9
document.getElementById('TextBoxID').readOnly = true;    //to enable readonly


document.getElementById('TextBoxID').readOnly = false;   //to  disable readonly
Absolutism answered 24/7, 2013 at 5:12 Comment(0)
A
4
document.getElementById("").readOnly = true
Aldus answered 24/7, 2013 at 5:4 Comment(0)
A
4

Try This :

document.getElementById(<element_ID>).readOnly=true;
Angi answered 24/7, 2013 at 5:5 Comment(0)
S
2

This is another working example with jQuery:

$(document).ready(function() {
    $('.yourClassName').prop('readonly', true);
});
Stefan answered 24/4, 2023 at 13:58 Comment(0)
B
0

<!DOCTYPE html>
<html>
<body>
<input id="balloons" type="number" step="10" min="1" max="1000" size="25" value="60" > 
<input id="bloquear" type="checkbox" onclick="validate()" />

<p id="demo1"></p>
<p id="demo2"></p>

<script type=text/javascript>

  function validate(){

  document.getElementById("bloquear").checked == (bloquear.checked == 1 ? false : true );
  
  document.getElementById("demo1").innerHTML = bloquear.checked;
  document.getElementById("demo2").innerHTML = balloons.readOnly;
  
  if (balloons.readOnly) document.getElementById("balloons").removeAttribute("readonly"); 
  else balloons.setAttribute("readonly", "readonly");
  
}
</script>
</body>
</html>
Bailiff answered 27/4, 2022 at 20:26 Comment(0)
E
-5

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>
Egerton answered 12/11, 2014 at 16:32 Comment(0)
H
-6

I think you just have readonly="readonly"

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

Holloweyed answered 13/4, 2019 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.