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);
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>
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");
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With