2 ms·
I have this code: $.fn.setAttr = function(attr, bool) { if(bool) this.attr(attr, attr); else this.removeAttr(attr); return this; }
by ars 16y ago
I have this code:
$.fn.setAttr = function(attr, bool) {
if(bool)
this.attr(attr, attr);
else
this.removeAttr(attr);
return this;
}
$.fn.disabled = function(bool) {
return this.setAttr('disabled', bool);
}
$.fn.checked = function(bool) {
return this.setAttr('checked', bool);
}
I assume I should change all attr to prop?
I think you should add two more helpers (like .val):
.checked() and .disabled() - with no parameters they return a boolean, and with a parameter you can pass in true/false to set the property.
Those two are probably the most commonly used properties and if you had them most people could probably ignore attr vs prop.
- dmethvin 16y agoThe blog entry mentions this, but perhaps without enough detail. Your `.setAttr()` exactly matches the same as the `.attr()` method's new behavior when it's passed a Boolean argument to set. The other two would be great suggestions for 1.7!