patternjavascriptMinor
Extend jQuery with enable/disable function
Viewed 0 times
extendenablewithdisablefunctionjquery
Problem
I want to create an extended disable/enable function in jQuery. This is working, but is this the best solution? Is each loop necessary?
$.fn.ariaDisabled = function (isDisabled) {
///
/// Sets the disabled state.
///
return this.each(function () {
var $item = $(this);
if (isDisabled === true) {
$item
.attr('disabled', 'disabled')
.attr('aria-disabled', 'true')
.addClass('disabled');
}
else {
$item
.removeAttr('disabled')
.removeAttr('aria-disabled')
.removeClass('disabled');
}
});
};Solution
Overall it looks quite nice.
Just a few points:
Edit
Per m90's comment the loop is per jQuery plugin conventions as noted here
Just a few points:
- The parameter name is asking a question about the element, not making a statement of what to do. I would rename it to
disablewhich enables the caller to better signify their intent.
- In the same vein, I would also rename the function to
ariaDisableas it performs an action as opposed to asking a question
Edit
Per m90's comment the loop is per jQuery plugin conventions as noted here
Context
StackExchange Code Review Q#77312, answer score: 5
Revisions (0)
No revisions yet.