5
\$\begingroup\$

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) {
 /// <summary>
 /// Sets the disabled state.
 /// </summary>
 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');
 }
 });
 };
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jan 12, 2015 at 12:02
\$\endgroup\$
1
  • \$\begingroup\$ If the function is called ariaDisabled then it should only change the [aria-disabled] attribute. \$\endgroup\$ Commented Jan 12, 2015 at 15:18

1 Answer 1

5
\$\begingroup\$

Overall it looks quite nice. 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 disable which enables the caller to better signify their intent.
  • In the same vein, I would also rename the function to ariaDisable as 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

answered Jan 12, 2015 at 12:38
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.