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.
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.