0

Some questions.

  1. Is there some CSS selector similar to :checked to "evaluate" if an input type="text" is empty or not?

  2. If there isn't, is there a Jquery alternative? To be more specific I want to stylize the following element after a "Non empty" input. I should probably only assign a class into this input: class="NotEmpty" and then prepare a CSS .NotEmpty+p {}, right?

  3. What could be the reason to have a checked selector and not other similar options?

Sean Branchaw
  • 597
  • 1
  • 5
  • 21
Rafael
  • 193
  • 1
  • 11
  • You may have a look at this url [link](http://stackoverflow.com/questions/5275857/highlight-label-if-checkbox-is-checked) – vignesh Jun 30 '16 at 19:58
  • 1
    You may want to take a look at this: http://stackoverflow.com/questions/1854556/check-if-inputs-are-empty-using-jquery – dokgu Jun 30 '16 at 20:03
  • No, in practice you need a script to accomplish that – Asons Jun 30 '16 at 20:04
  • Humm. Yeap. I was just thinking in terms of styling, but a "validation" aproach is interesting... – Rafael Jun 30 '16 at 20:05

2 Answers2

2

In practice such a selector would iterate over all elements.

So just do it yourself using filter:

var $collection = $("some selector").filter(function(){ return this.value });
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

jQuery doesn't have such selector. There is an :empty selector but it works by checking element's descendants.

As the other answer shows you can use the .filter method for checking the matching elements. But if you want to use a selector you can add one. Here is an example:

$.extend($.expr[':'], {
    empty_field: function(a) {
        return typeof a.value === 'string'
               && a.value.trim().length === 0;
    }
});

The selector can be used like the predefined selectors:

// select the empty inputs:
$('input:empty_field');
// select the not-empty inputs:
$('input:not(:empty_field)');
Ram
  • 143,282
  • 16
  • 168
  • 197