[jquery] how we add or remove readonly attribute from textbox on clicking radion button in cakephp using jquery?

Here is my cakephp generated HTML radio box and text box script:

<input type="radio" id="need_staff_on_site" name="data[CaterRequest][need_staff_on_site]" value="yes" class="staff_on_site"><span>Yes</span>

<input type="radio" id="need_staff_on_site" name="data[CaterRequest][need_staff_on_site]" class="staff_on_site" value="no"><span>No</span>

How many staff?<input type="text" maxlength="3" id="no_of_staff" name="data[CaterRequest][staff_needed]" class="txtboxSml2" readonly="readonly">

jquery Script:

$(document).ready(function(){
   $('.staff_on_site').click(function(){
   $arr=$(this).val();
   if($arr == "yes"){ $("#no_of_staff").removeAttr("readonly"); }
   if($arr == "no"){ $("#no_of_staff").attr("readonly", "readonly"); }
  });
});

Demo jsfiddle Link

This question is related to jquery cakephp

The answer is


In your Case you can write the following jquery code:

$(document).ready(function(){

   $('.staff_on_site').click(function(){

     var rBtnVal = $(this).val();

     if(rBtnVal == "yes"){
         $("#no_of_staff").attr("readonly", false); 
     }
     else{ 
         $("#no_of_staff").attr("readonly", true); 
     }
   });
});

Here is the Fiddle: http://jsfiddle.net/P4QWx/3/


You could use prop as well. Check the following code below.

$(document).ready(function(){

   $('.staff_on_site').click(function(){

     var rBtnVal = $(this).val();

     if(rBtnVal == "yes"){
         $("#no_of_staff").prop("readonly", false); 
     }
     else{ 
         $("#no_of_staff").prop("readonly", true); 
     }
   });
});