2

How to count text fields that are not empty with JQuery?

Here's how I thought:

var cnt = 0;

$.each($("input[name=items]"), function(i){
   if($(this).val() != ""){
       cnt++;
   } 
});

If I use this codes, I have to write each function unconditionally. This is a waste of somehow, but is there any other way? Or is this the best way?

3
  • Why do you think the existing code is bad? Commented Sep 12, 2018 at 6:46
  • @AnkitAgarwal No. I just wanted to know if there is another way besides the above code Commented Sep 12, 2018 at 6:53
  • @undefined Sorry. I got a typo in the code and fixed it! Commented Sep 12, 2018 at 6:55

4 Answers 4

6

There's no selector to retrieve empty input elements, so the only way to achieve what you require is through a loop. You can use an implicit loop, though, by using filter() instead of each(), like this:

var cnt = $('input[name="items"]').filter(function() {
  return this.value.trim() == '';
}).length;

If you don't need IE support, then this code can be shortened using an ES6 arrow function:

var cnt = $('input[name="items"]').filter((i, el) => el.value.trim() === '').length;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I was wondering if there was a way to select an empty text field without just writing a loop statement.
As I mentioned in the answer, you'll need a loop. An implicit loop such as this is the most succinct logic you're going to get.
I just wondered if there was another way. It's okay to use a loop statement. Thanks again for the answer.
0

Length can be get by a query selector

$('input[name="items"][value=""]').length

1 Comment

This is assuming that the value hasn't been changed by the user since the page was loaded. Given this seems to be for form validation, then that's a little redundant.
0

You can use just a CSS selector for that:

$('input[name=items][value=""], input[name=items]:not([value=""])').length

I have combined two selectors together cause if the element doesn't contain the value attribute at all you will get the result 0 and for such cases need input[name=items]:not([value=""])

Comments

0

Another method that doesn't require constructing an intermediate array nor a jQuery collection is with reduce:

const count = Array.prototype.reduce.call(
  $("input[name=items]"),
  (a, input) => $(input).val() === '' ? a + 1 : a,
  0
);
console.log(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="items">
<input name="items">
<input name="items" value="foobar">
<input name="items">

You don't need jQuery for this though, since all you really need is to select the matching elements, which you can do with querySelectorAll:

const count = Array.prototype.reduce.call(
  document.querySelectorAll("input[name=items]"),
  (a, input) => input.value === '' ? a + 1 : a,
  0
);
console.log(count);
<input name="items">
<input name="items">
<input name="items" value="foobar">
<input name="items">

2 Comments

I am developing back-end only, so the jquery syntax is somewhat unfamiliar. I appreciate your answer and I'll check it out a bit more.
See edit, there's absolutely no need for jQuery here if you don't feel comfortable with it

Your Answer

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.