Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a textarea field with name attribute using jquery [duplicate]

Tags:

html

jquery

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);
like image 379
Neelu Avatar asked Sep 26 '15 04:09

Neelu


2 Answers

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>
like image 85
Tushar Avatar answered Oct 09 '22 23:10

Tushar


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"); 
    }
  });
});
like image 23
Ankur Mahajan Avatar answered Oct 09 '22 21:10

Ankur Mahajan