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.
Try This :
Here you have example how to set the readonly attribute:
You can get the input element and then set its
readOnly
property totrue
as follows:Specifically, this is what you want:
Call this
onLoadBody()
function on body tag like:View Demo: jsfiddle.
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.
I think you just have readonly="readonly"
<html><body><form><input type="password" placeholder="password" valid="123" readonly=" readonly"></input>