Jacob Chong Jacob Chong - 7 months ago 18
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...

<?php
include('session.php');
?>

<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"/>
</div>
</td>



Answer

@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 fields=["CIDTYP","CICOUN","CIDNUM","CIDIS8","CIDXP8","CMSALT","CMCNA1","CMCNA2","CSCSEX","CSCMST","CSRACE","CSCITZ","CSBIRD","CSSTAF","CSRMGR","CAADTY","CAAFMT"] 
            var x, l=fields.length;
            var fieldname;
            document.getElementById("btnOK").value="Approve";
                for(x=0; x<l; x++){
                    fieldname=fields[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 {}
}