Is it possible to make a finish in the selector?

I would like to consider all inputs in my form as empty. With empty, I mean that its value is empty after trimming its value (if the user inserts spaces also empty). This jquery counts them, but does not include cropping:

$(':text').filter('[value=""]').length

Is there something jquery that can be used to trim the selector?

thank

+3
source share
2 answers
$(':text').filter(function () {
    return $.trim($(this).val()).length === 0;
});
+11
source

While the method.filter() in @Domenic's answer is a good approach, I would implement it a little differently.

Example: http://jsfiddle.net/patrick_dw/mjuyk/

$('input:text').filter(function () {
    return !$.trim(this.value);
});
  • input:text. jQuery DOM, , type='text'. (. .
  • , this.value, $(this).val().
  • length, . !.

.filter(), .not():

: http://jsfiddle.net/patrick_dw/mjuyk/1/

$('input:text').not(function () {
    return $.trim(this.value);
});

, , :

: http://jsfiddle.net/patrick_dw/mjuyk/2/

$.extend($.expr[':'], {
   novalue: function(elem, i, attr){
      return !$.trim(elem.value);
   }
});

$('input:text:novalue')
+9

Source: https://habr.com/ru/post/1776767/


All Articles