1

The code in the following snippet demonstrates that after changing the disabled attribute, I can't retrieve the original attribute value, at least for the disabled attribute. The jQuery docs imply that element.getAttribute() should be able to retrieve the original value.

enter image description here

However, it doesn't detect that the select was originally not disabled.

So, are the docs wrong? Are boolean attributes different? Most importantly, is there a way to get the original value after it's been changed with prop()?

NOTE I'm using jQuery 1.8.3 and it's being interpreted by Chromium 37 in Opera.

$('button').on('click', function() {
  var $inputs = $('input, select');
  $inputs.each(function() {
    var $this = $(this);
    var name = $this.prop('name');
    console.log('before changing ' + name + '...');
    console.log("\tgetAttribute: " + $this[0].getAttribute('disabled'));
    console.log("\tprop: " + $this.prop('disabled'));
    console.log("\tattr: " + $this.attr('disabled'));

    $this.prop('disabled', true);

    console.log('after changing ' + name + '...');
    console.log("\tgetAttribute: " + $this[0].getAttribute('disabled'));
    console.log("\tprop: " + $this.prop('disabled'));
    console.log("\tattr: " + $this.attr('disabled'));
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<button>Click</button>
<input name="input" type="text" disabled='disabled' />
<select name="select">
  <option>Option</option>
  <option>Option</option>
  <option>Option</option>
</select>

EDIT

Unfortunately, the question at .prop() vs .attr() doesn't actually answer the question when it comes to boolean properties like disabled. Consider this fiddle: http://jsfiddle.net/garreh/uLQXc. It works fine under 1.8.3. Now, consider this fork, which alters 'disabled' instead of 'blah': http://jsfiddle.net/wrn1ryjq/1. The input is originally NOT disabled. After being altered, even attr returns 'disabled'. So, the stock answer that attr returns the original value does not appear to be true. My question still stands: after altering it with prop, how do I find out the original state of disabled?

EDIT Well that's embarrassing. Of course attr() won't retrieve the original value. The docs say that it won't. The real question is, how to get the original value of disabled from an input after disabling with with prop.

Unfortunately, according to this comment, it's not possible :/ Thanks for the suggestions though.

Community
  • 1
  • 1
Charles Wood
  • 864
  • 8
  • 23

1 Answers1

0

Since, prop works only when the element does have that property, you are not going to get any value returned by it when it comes to those input fields which are not disabled.

The same problem is with using attr as it would check for the element to have that specific attribute.

Instead, I would suggest you to use $('#someId:disabled').length to see if the element you are looking for has a disabled attribute with it or not. The selector will be able to find only those elements which has a disabled attribute with them and hence it will either result in a 0 or a 1.

See this fiddle.

HTML

<input id="input1" disabled />
<input id="input2" disabled="true" />
<input id="input3" />

Javascript

$('#result').append($('#input1:disabled').length);
$('#result').append($('#input2:disabled').length);
$('#result').append($('#input3:disabled').length);
Aniket
  • 9,622
  • 5
  • 40
  • 62
  • Does this return the same results if you then disable input 3 with prop()? – Charles Wood Sep 27 '14 at 14:12
  • @CharlesWood Yes! If you disable '#input3' using `.prop('disabled', true)`, the final check will return a `1` and not a `0`. You can see for yourself in the updated example - http://jsfiddle.net/aniketpant/kf7dve0h/1/ – Aniket Sep 27 '14 at 15:12
  • Ah. No, I need a way to find out if it was originally disabled on page load or not. Thanks though. – Charles Wood Sep 29 '14 at 14:11
  • @CharlesWood Why don't you store the original state on page load? – Aniket Sep 29 '14 at 14:16
  • Looks like I'll be doing that. It's just annoying that `disabled` is a special case. – Charles Wood Sep 29 '14 at 14:18