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:
Make sure you use <body onload="onLoadBody();">
for anyone using this in the future.
This question is related to
javascript
input
readonly
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.
Here you have example how to set the readonly attribute:
<form action="demo_form.asp">_x000D_
Country: <input type="text" name="country" value="Norway" readonly><br>_x000D_
<input type="submit" value="Submit">_x000D_
</form>
_x000D_
Try This :
document.getElementById(<element_ID>).readOnly=true;
<html><body><form><input type="password" placeholder="password" valid="123" readonly=" readonly"></input>
Source: Stackoverflow.com