Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make field readonly - checkbox - jQuery

I have this checkbox:

<input type="checkbox" name="suspended" id="s" value="<?php echo $udata['suspended']; ?>" <?php echo $suspended; ?>>

And this textarea:

<textarea name="suspendreason" id="sr" style="height:60px" class="field"><?php echo $udata['suspendreason']; ?></textarea>

My question is, how can I make the textarea READONLY whenever the #s checkbox is unchecked?

like image 294
Oliver 'Oli' Jensen Avatar asked Sep 14 '11 16:09

Oliver 'Oli' Jensen


People also ask

How do I make a checkbox readonly?

A checkbox HTML element doesn't have a "readonly" property. Consequently, the only way to make a checkbox appear to be "readonly" is to disable the control.

How do you make a field readonly in JQuery?

To make a textarea and input type read only, use the attr() method .

How can I make a checkbox not clickable in JQuery?

Just add onclick="return false" in your HTML code.

Can a checkbox be checked and disabled?

In checkboxes, for example, you can check them on or off (thus setting the CHECKED state) but you don't change the value of the field.


3 Answers

With your requirement, something like this:

$(document).ready(function(){
    if($('#s:checked').length){
        $('#sr').attr('readonly',true); // On Load, should it be read only?
    }

    $('#s').change(function(){
        if($('#s:checked').length){
            $('#sr').attr('readonly',true); //If checked - Read only
        }else{
            $('#sr').attr('readonly',false);//Not Checked - Normal
        }
    });
});
like image 177
cillierscharl Avatar answered Nov 17 '22 22:11

cillierscharl


I recommend making it readonly and disabled.

$("#sr").attr('disabled', 'disabled');
$("#sr").attr('readonly', 'readonly');

simply because if someone is in Carat Browsing mode, rare, but yes I've had clients do this, they can edit the disabled fields.

And for the checkbox:

<script type="text/javascript">
    $(document).ready(function () {
        // check upon loading
        if(!$("#s").is(:checked))
        {
           $("#sr").attr('disabled', 'disabled');
           $("#sr").attr('readonly', 'readonly');
        }

        // event
        $("#s").change(function () {
           if(!$(this).is(:checked)) {
           {
              $("#sr").attr('disabled', 'disabled');
              $("#sr").attr('readonly', 'readonly');
           }
           else
           {
              $("#sr").attr('disabled', '');
              $("#sr").attr('readonly', '');
           }
         }
      });
</script>
like image 22
Karyna Kerin Avatar answered Nov 17 '22 22:11

Karyna Kerin


$(document).ready(function() {
    $("#s").change(function() {
        if (!this.checked) {
            $("#sr").attr('disabled', 'disabled');
            $("#sr").attr('readonly', 'true');
        }
        else {
            $("#sr").removeAttr('disabled');
            $("#sr").removeAttr('readonly');
        }
    });

    //triger change event in case checkbox checked when user accessed page
    $("#s").trigger("change")
});

Working demo - http://jsfiddle.net/cc5T3/1/

like image 3
ipr101 Avatar answered Nov 18 '22 00:11

ipr101