PMay 1903
PMay 1903

Reputation: 1143

How to disable textboxs in radio button with Javascript

My function is


Image


Demo

HTML:

<table>
<tr>
    <td>
        <input type="radio" name="vehicle" value="company_vehicle" />Company Vehicle</td>
</tr>
<tr class="set_width" id="company_vehicle">
    <td>
        <input type="text" />
    </td>
    <td>
        <input type="checkbox" />Company Vehicle</td>
</tr>
<tr>
    <td>
        <input type="radio" name="vehicle" value="hiring_vehicle" />Hiring Vehicle</td>
</tr>
<tr class="set_width" id="hiring_vehicle">
    <td>
        <input type="text" />
    </td>
    <td>
        <input type="radio" name="abc" value="car" />Car</td>
    <td>
        <input type="radio" name="abc" value="bus" />Bus</td>
</tr>
<tr>
    <td>
        <input type="radio" name="vehicle" value="taxi" />Taxi</td>
</tr>
<tr class="set_width" id="taxi">
<td>
        <input type="checkbox" />Taxi</td>
</tr>
</table>

Javascript:

var rbt_vehicle = $("input[name=vehicle]");

$(rbt_vehicle).on('change', function (e) {
var valueSelected = this.value;

$('#' + valueSelected).find(':text').prop('disabled', false).val('').removeClass('readonly').closest('tr').siblings('tr.set_width').find(':text').prop('disabled', true).addClass('readonly');
$('#' + valueSelected).find(':checkbox,:radio').prop('disabled', false).closest('tr').siblings('tr.set_width').find(':checkbox,:radio').prop('disabled', true);
});

Upvotes: 0

Views: 69

Answers (2)

wvandaal
wvandaal

Reputation: 4295

Using your current function, you can actually remove a lot of the current code and just do the following:

var rbt_vehicle = $("input[name=vehicle]");

$(rbt_vehicle).on('change', function (e) {
    var valueSelected = this.value;

    // your other code here ...

    $('input[type="text"]').prop('disabled', valueSelected === 'taxi');

});

DEMO

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

do like this:

$('input.rdo').click(function(){
if(this.id == 'rdoTaxi')
{
    $('.textbox').prop('disabled',true);
}
    else
    {
        $('.textbox').prop('disabled',false);
    }

});

Fiddle DEMO

Upvotes: 2

Related Questions