Jacob Chong Jacob Chong - 2 years ago 106
Javascript Question

Conditionally set editable or readonly text field

How to conditionally enable or disable to editable field (edit/read only)? I used session role from database to make condition on the text field. However, I don't know how to proceed it...


<tr class="row1">
<td width="30%"><div align="left">Customer name1</div></td>
<td width="100%">
<div align="left">
<input type="text" name="CMCNA1" id="CMCNA1" style="width:100%;" pattern="[A-Za-z]+" title="Please insert alphabetical only" maxlength="35"/>

Answer Source

@Narayan, This is what i did, n functioning... However i am not sure whether what i did on the radio button disable is true method, because radio button unable to set readonly.

function makerCheckerField()
    var role= "<?php echo $_SESSION['login_user_role']; ?>";
    var maker="Checker";
        if (role == maker){   
            var x, l=fields.length;
            var fieldname;
                for(x=0; x<l; x++){
                    document.getElementById(fieldname).setAttribute("readonly", true);
            var radios0 = document.form0.CSCCAT;
                for (var i=0, iLen=radios0.length; i<iLen; i++) {
                radios0[i].disabled = true;
            var radios1 = document.form0.CAANSF;
                for (var i=0, iLen=radios1.length; i<iLen; i++) {
                radios1[i].disabled = true;
        } else {}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download