Reputation: 223
I have a group of radio buttons with the lase option "Other" and an input field type text
. By default the input field is disabled, if you click on "Other" radio button the field is then enabled. How do I disable the input field if the select on radio button is changed?
This is my code:
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});
this code is not disabling the input field if you change the radio select.
UPDATE 1 I'm trying this:
$(document).ready(function(){
$("#text-inputt").prop('disabled', true);
});
$("#lastt").click(function(){
if($(this).is(':checked')) {
$("#text-inputt").prop("disabled", false);
} else {
$("#text-inputt").prop("disabled", true);
}
});
But is not setting the input to disable if you change the radio button.
Upvotes: 0
Views: 3875
Reputation: 1871
A solution could be:
change
listener to your radio button groupWorking example: https://jsfiddle.net/3afjqe7j/1/
var $input = $('#text-inputt');
$("input[name=my-radios]").on('change', function() {
var disabled = $(this).val() !== 'other';
$input.prop('disabled', disabled);
});
Upvotes: 0
Reputation: 8537
You can do it like this
$(document).ready(function(){
$("#text-inputt").prop('disabled', true);
$('input[type=radio]').click(function(){
if($(this).prop('id') == "lastt"){
$("#text-inputt").prop("disabled", false);
}else{
$("#text-inputt").prop("disabled", true);
}
});
});
Upvotes: 1
Reputation: 44
Try this , Here Input box will toggle ie Enable/Disbale
$("#radioBtn").change(function(){
$("#inputBox").attr("disabled", ! $(this).is(':checked'));
});
Upvotes: 0
Reputation: 542
change :
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});
to
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){
if($(this).val=='Other')
{
$("#text-inputt").prop('disabled', false)
}
else
{
$("#text-inputt").prop('disabled', true)
}
});
Upvotes: 0