22

Can I in some way select all input submit elements that are not disabled?

I can easily find all the disabled ones with: http://api.jquery.com/disabled-selector/

$("input.saveitembtn:disabled")

but is there something a'la:

$("input.saveitembtn:NOTdisabled")

My solution until now is to run through them all with jQuerys .each using .is to check each one individually:

$("input.saveitembtn").each(function(a){
  if( !$(this).is(':disabled') ) {
    ...
  }
});

which I find as total overkill. Is there a simple selector in jQuery?

Steeven
  • 4,057
  • 8
  • 38
  • 68

3 Answers3

42

Yes, there is :not()

$("input.saveitembtn:not(:disabled)")
palaѕн
  • 72,112
  • 17
  • 116
  • 136
13

Not sure why but the accept answer does not work for me. However this does:

$("input.saveitembtn:not([disabled])");
  • 3
    This is could be the reason (from the [jquery-api-doc](https://api.jquery.com/disabled-selector/)): _"Although their resulting selections are usually the same, the `:disabled` selector is subtly different from the `[disabled]` attribute selector; `:disabled` matches elements that are actually disabled while `[disabled]` only checks for the existence of the disabled attribute."_ What the HTML-Standard considers as an "actually disabled" form control can be looked up here: [The disabled attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled)... – Mayinx Jan 10 '19 at 23:17
  • @Mayinx - a few years late here but yes, I'm pretty sure when I posted that I was "disabling" an anchor element, so your explanation is spot on. – But those new buttons though.. Jan 19 '20 at 17:17
2

Vanilla JavaScript:

const ELS_enabled = document.querySelectorAll("input:not([disabled])");

console.log(ELS_enabled.length); // 2
<input type="text" required placeholder="Required">
<input type="text" required disabled placeholder="Required and disabled">
<input type="text" required placeholder="Required">
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313