2

I have a dropdown with a few disabled options. And I want make all options enabled.

This is html:

<select id="selectId">
    <option value="JavaScript" disabled="">JavaScript</option>
    <option value="Angular">Angular</option>
    <option value="Backbone" disabled="">Backbone</option>
</select>

JavaScript:

var select = $("#selectId");
select.find("option").each(function(index, item) {
  item.attr('disabled',false);
});

But I get an error: TypeError: item.attr is not a function. What's wrong here?

Liam
  • 27,717
  • 28
  • 128
  • 190
user2598794
  • 697
  • 2
  • 10
  • 23
  • Use `$(this).attr('disabled',false);` in loop or just ` $("#selectId option").removeAttr("disabled");` without any loop! – Prashant Shirke Jun 15 '17 at 09:53
  • @PrashantShirke Yes, it's working and also $(item).attr('disabled',false); Just curious, why my initial version is not working? – user2598794 Jun 15 '17 at 09:59
  • `attr` is jquery method so works only on jquery objects. `item` in `.each` is DOM element. – Prashant Shirke Jun 15 '17 at 10:00
  • Possible duplicate of [Remove disabled attribute using JQuery?](https://stackoverflow.com/questions/13626517/remove-disabled-attribute-using-jquery) – Liam Jun 15 '17 at 10:13

5 Answers5

6

The correct way to alter properties (disabaled is a property not an attribute) is to use prop, see .prop() vs .attr():

$("#selectId option").prop('disabled', false);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="selectId">
    <option value="JavaScript" disabled="">JavaScript</option>
    <option value="Angular">Angular</option>
    <option value="Backbone" disabled="">Backbone</option>
</select>

You can set .attr('disabled',false); but this doesn't work on every HTML element. The correct way to remove properties (disabled is a property and not an attribute) is prop.

Your each also doesn't return a jquery object, it returns a vanilla DOM element, hence the TypeError: item.attr is not a function.. item does not have a attr function because it's not a jquery object.

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Neither is working: TypeError: $(...).prop is not a function – user2598794 Jun 15 '17 at 10:03
  • Have you loaded jquery? What version of jquery are you using? – Liam Jun 15 '17 at 10:04
  • FYI `disabled="false"` makes sense! – Prashant Shirke Jun 15 '17 at 10:13
  • @PrashantShirke no it doesn't, disabled is a property, it's usage is implied by its' existance or lack of. The equals part is never read. An option is either `` or ``. `` is invalid HTML (though most browsers will handle it, kinda) – Liam Jun 15 '17 at 10:34
  • See [Correct value for disabled attribute](https://stackoverflow.com/questions/6961526/correct-value-for-disabled-attribute) – Liam Jun 15 '17 at 10:37
2

var select = $("#selectId");
select.find("option:disabled").prop("disabled",false)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectId">
    <option value="JavaScript" disabled="">JavaScript</option>
    <option value="Angular">Angular</option>
    <option value="Backbone" disabled="">Backbone</option>
</select>
  1. No need to iterate.
  2. Use selector :disabled to select all disabled
  3. Use .prop() to set to enabled
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
guradio
  • 15,524
  • 4
  • 36
  • 57
1

Your initial version is not working since your selector select.find("option") returns a jQuery object known as the "wrapped set", which is an array-like structure that contains all the selected DOM elements. This elements are not jQuery objects so attr() method will not work and that is why you get TypeError: item.attr is not a function.

UPDATE: For @Liam, this will work with .attr('disabled', false) as you can see from the code below. But I still prefer using .prop().

var select = $("#selectId");
select.find("option").each(function(index, item) {
  $(item).attr('disabled', false);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="selectId">
    <option value="JavaScript" disabled="">JavaScript</option>
    <option value="Angular">Angular</option>
    <option value="Backbone" disabled="">Backbone</option>
</select>
Stanislav Kvitash
  • 4,614
  • 18
  • 29
1

Using jQuery-3.5.1, this worked for me:

$("#selectId").find("option:disabled").removeAttr('disabled');
Fjodr
  • 919
  • 13
  • 32
-1

This one should work fine :)

$("#selectId option").each((i,items)=>{
        $(items).attr('disabled',false)
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectId">
        <option value="JavaScript" disabled="">JavaScript</option>
        <option value="Angular">Angular</option>
        <option value="Backbone" disabled="">Backbone</option>
    </select>
Simon L.
  • 71
  • 5
  • Wow!!! it seems that i messed something up in a terrible way. Maybe some one can explain the down vote and prevent me in doing other people more harm then necessary – Simon L. Jul 06 '17 at 14:06
  • --> According to my terrible answer – Simon L. Jul 06 '17 at 14:14