Custom Content script to set something Read Only

Hello Community, we are currently on version 2017 r3 and running into a little problem. On edit I would like FieldA to be set to ReadOnly when I toggle FieldB's checkbox in real time and not after save.

I've tried the below example and had no luck, anyone have any other examples of how i might accomplish this?


<script for=window event=onload>

if(document.getElementById('FieldB').checked)

{

document.getElementById('FieldA').readOnly=true;

}

else

{

document.getElementById('FieldA').readOnly=false;

}

</script>