7

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);
0

4 Answers 4

9

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>

Sign up to request clarification or add additional context in comments.

Comments

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"); 
    }
  });
});

1 Comment

You're showing how to disable other elements except textarea.
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");

Comments

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.