0

I have the following code that unlocks the dropdown by removing the disabled attribute with the removeAttr function. This example doesn't work in Mozilla Firefox 24 for Ubuntu. However it works perfectly fine if added an alert after the RemoveAttr function as shown in the next example:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

Working example:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {

            $('#dropdown2').removeAttr('disabled');
            alert("REMOVED");
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);
        }
        else
        {
            $('#dropdown2').attr('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });

Example with .prop doesn't work as well:

        $("#dropdown1").change(function() {
        $('#dropdown2').find('option').remove().end();
        if (obj[$(this).val()] !== undefined)
        {
            $('#dropdown2').prop('disabled', false);
            $('#dropdown2').append('<option></option>' + obj[$(this).val()]);
            $('#dropdown2').attr('required', true);

        }
        else
        {
            $('#dropdown2').prop('disabled', true);
            $('#dropdown2').attr('required', false);
        }

    });
user1029829
  • 941
  • 3
  • 16
  • 34
  • There is great documentation about .attr() method... `To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.` Just FYI, don't use removeProp() to replace removeAttr(), set property to false instead – A. Wolff Sep 26 '13 at 13:51
  • 2
    can you share the related html – Arun P Johny Sep 26 '13 at 13:58
  • and the value of `obj` – Arun P Johny Sep 26 '13 at 13:59
  • If the problem remains you should create a http://jsfiddle.net reproducing this problem – Itay Sep 26 '13 at 14:02
  • All right, it looks like the Select2 plugin is messing up with those dropdowns. As soon as removed, everything works fine. – user1029829 Sep 26 '13 at 14:46
  • http://jsfiddle.net/DcunN/7/ When you remove the Select2 from javascript, it unblocks the second dropdown with no problems. – user1029829 Sep 26 '13 at 15:10

3 Answers3

4

You should use .prop() to set the disabled property status

to enable

$('#dropdown2').prop('disabled', false);

to disable

$('#dropdown2').prop('disabled', true);

Read: Attributes vs. Properties

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 1
    Same thing, still doesn't work: $("#dropdown1").change(function() { $('#dropdown2').find('option').remove().end(); if (obj[$(this).val()] !== undefined) { $('#dropdown2').prop('disabled', false); $('#dropdown2').append('' + obj[$(this).val()]); $('#dropdown2').attr('required', true); } else { $('#dropdown2').prop('disabled', true); $('#dropdown2').attr('required', false); } }); – user1029829 Sep 26 '13 at 13:56
3

From the (documentation](http://api.jquery.com/attr/#entry-longdesc):

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

So you should use the .prop() function instead.

$('#dropdown2').prop('disabled', false); // Enables the element
$('#dropdown2').prop('disabled', true ); // Disables the element
Itay
  • 16,601
  • 2
  • 51
  • 72
1

use prop

$('#dropdown2').prop('disabled', false);

disable true make disable the element and disable false Enable the element

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79