Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable Textbox in jquery

I want to enable text box when first radio button is checked & if second then i want to disable textbox here is my code that is not working please help..

<label class="radio-inline">
    <input type="radio" value="Y" name="IsLive" class="grey">
    YES
</label>
<label class="radio-inline">
    <input type="radio" value="N" name="IsLive" class="grey">
    NO
</label>

<label class="col-sm-2 control-label">
    Live Date
</label>
<div class="col-sm-3">
    <input type="text" id="LiveDate" name="LiveDate" class="form-control date-picker">
</div>
$('input:radio[name="IsLive"]').change(function () {
    if ($(this).is(':checked') && $(this).val() == 'Y') {
        // append goes here
        $("#LiveDate").show();
    } else {
        $("#LiveDate").prop("disabled", true);
    }
});
like image 806
Patel Jack Avatar asked Mar 12 '23 17:03

Patel Jack


1 Answers

You can achieve this by just setting the disabled property based on whether or not the value of the chosen radio is Y. Try this:

$('input:radio[name="IsLive"]').change(function() {
    $("#LiveDate").prop("disabled", this.value != 'Y');
});

Working example

Note that I removed the :checked condition because it's redundant for a radio button, as to raise a new change event the element must be checked.

like image 119
Rory McCrossan Avatar answered Mar 16 '23 07:03

Rory McCrossan