Pooja Desai
Pooja Desai

Reputation: 227

How to enable or disable drop-down or text-box on selection other drop-down?

<script>
$('#Select_Font').attr('disabled', 'true');
$('#Enter_Text').attr('disabled', 'true');
$('.dropdownstyle').change(function(){
    $(".dropdownstyle option:selected").text();
    if($(this).attr('value').match(/Custom$/)) 
    {
        $('#Select_Font').attr('disabled', 'false');
        $('#Enter_Text').attr('disabled', 'false');
    } else if($(this).attr('value').match(/no Thanks$/)) 
    {
        $('#Select_Font').attr('disabled', 'true');
        $('#Enter_Text').attr('disabled', 'true');
    }
});
</script>


<table>
<tbody>
<tr>
<td>
<span>Select type</span>
</td>
<td>
<select class="dropdownstyle">
    <option value="No Thanks">No Thanks</option>
    <option value="Custom lid (200)">Custom lid (200)</option>
</select>
</td>
</tr>
<tr>
<td>
<span>Select font</span>
</td>
<td>
<select id="Select_Font"> 
    <option value="Arial">Arial</option> 
    <option value="georgia">georgia</option> 
</select>
</td>
</tr>
<tr>
<td>
<span>enter text</span>
</td>
<td>
<input type="text" id="Enter_Text">
</td>
</tr>
</tbody>
</table>

In above code initially I want font drop-down and text-box disable but on select of custom form first drop-down I want font drop-down and text box to be enable. I am not sure where I am going wrong, so what should be done to make it work.

Upvotes: 0

Views: 2009

Answers (3)

Anujith
Anujith

Reputation: 9370

Try: SAMPLE

$('#Select_Font, #Enter_Text').attr('disabled', 'disabled');
$('.dropdownstyle').change(function () {
    var present = $(this).val().match(/\Custom\b/gi) == null ? true : false;
    if(present){
        $('#Select_Font, #Enter_Text').prop('disabled', 'disabled');
    }
    else{
        $('#Select_Font, #Enter_Text').removeAttr('disabled');
    }
});

$(this).attr('value') will return undefined. You should use $(this).val()

Upvotes: 1

emco
emco

Reputation: 4719

You're using a string ('true', 'false') instead of a boolean (true, false). Try this:

<script>
$('#Select_Font').attr('disabled', true);
$('#Enter_Text').attr('disabled', true);
$('.dropdownstyle').change(function(){
    $(".dropdownstyle option:selected").text();
    if($(this).attr('value').match(/Custom$/)) 
    {
        $('#Select_Font').attr('disabled', false);
        $('#Enter_Text').attr('disabled', false);
    } else if($(this).attr('value').match(/no Thanks$/)) 
    {
        $('#Select_Font').attr('disabled', true);
        $('#Enter_Text').attr('disabled', true);
    }
});
</script>
....

Upvotes: 1

bgusach
bgusach

Reputation: 15205

You should use:

$("#id").prop("disabled", true);

Or this also should work:

$("#id")[0].disabled = true;

In your code you are passing true and false as a string (first error) to the .attr() which is no longer used (second error)

Upvotes: 0

Related Questions