5

I have a textarea that I want to disable when some condition is met, else it will be enabled.

<textarea name='example'>I want to disable this</textarea>

I tried this method, but it's not working:

$('#example').attr('disabled', true);
Tushar
  • 78,625
  • 15
  • 134
  • 154
Neelu
  • 83
  • 1
  • 6

4 Answers4

7

Use attribute-value selector

To disable a textarea

$('textarea[name="example"]').prop('disabled', true); // disable

To enable

$('textarea[name="example"]').prop('disabled', false); // enable

Demo

$('#myButton').on('click', function() {
  var currentState = $(this).text();
  $('textarea[name="example"]').prop('disabled', currentState === 'Disable');
  $(this).text(currentState === 'Enable' ? 'Disable' : 'Enable');
});
button {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<textarea name='example' disabled>I want to diable this</textarea>

<button id="myButton">Enable</button>
аlex dykyі
  • 4,154
  • 23
  • 36
Tushar
  • 78,625
  • 15
  • 134
  • 154
1

Use $("#textbox1").attr("disabled", "disabled"); to disable your text box.

Demo

HTML

<span id="radiobutt">
  <input type="radio" name="rad1" value="1" />
  <input type="radio" name="rad1" value="2" />
  <input type="radio" name="rad1" value="3" />
</span>
<div>
  <input type="text" id="textbox1" />
  <input type="checkbox" id="checkbox1" />
</div>

Javascript

$("#radiobutt input[type=radio]").each(function(i){
  $(this).click(function () {
    if(i==2) { //3rd radiobutton
       $("#textbox1").attr("disabled", "disabled"); 
       $("#checkbox1").attr("disabled", "disabled"); 
    }
    else {
       $("#textbox1").removeAttr("disabled"); 
       $("#checkbox1").removeAttr("disabled"); 
    }
  });
});
Ankur Mahajan
  • 2,676
  • 2
  • 25
  • 38
0
$('textarea[name="example"]').prop("disabled", true);

Visit http://api.jquery.com/prop/

Kaushik Maheta
  • 1,545
  • 1
  • 15
  • 27
0

you should use the .prop() function with attribute-value selector

$('textarea[name="example"]').prop('disabled', true);
//$('textarea[name="example"]').prop('disabled', false);

Note: For jQuery 1.6+

or

$('textarea[name="example"]').attr("disabled","disabled");
Sathish
  • 2,404
  • 1
  • 9
  • 25