Click here to Skip to main content
14,216,355 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi!!!
need some help!!

I get total number of textboxes (itemtemplate) in a gridview using this code

$("#grdTemas tr").each(function () {
       var txtNum = $(this).find("input[id*='txtValoracionTema']");
       var cntText = $("#grdTemas input:text").length;

       txtNum.click(function () {
           alert(cntText);
       });


but i need get total count only of texboxes having a value(ignoring empty textboxes)

help!!!
thanks!
Posted
Comments
sudevsu 19-Mar-15 12:16pm
   
Check these sites.

http://forums.asp.net/t/1917519.aspx?Gridview+item+template+column+total+using+jquery+or+javascript

http://www.tagwith.com/question_144673_getting-textbox-value-inside-gridview-using-jquery-val-not-working/
Sergey Alexandrovich Kryukov 19-Mar-15 14:31pm
   
Excuse me, why posting links on somewhat close, but essentially irrelevant topics, instead of answering to the point? If you understand the articles/answers you are referencing, probably you know how to solve this simple problem. There is nothing wrong if you try to save your time, but you still spend some, and the use of it does not pay off. This is just the idea for you. Please see my answer for an example...
—SA
Maciej Los 19-Mar-15 12:54pm
   
PHP tag is missing ;)
Sergey Alexandrovich Kryukov 19-Mar-15 14:27pm
   
This is JavaScript using jQuery. Please see my answer.
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The idea of your implementation is completely unclear. First, let me illustrate the right idea on the sample finding the number of non-empty text boxes on the whole page.
First, find the set of jQuery wrappers represented text boxes:
var allTextBoxes = $("[type='text']");

In rare cases when you may have elements other then input also showing type="text", you can also add the element selector and combine it with the above, using multiple selector syntax:
var allTextBoxes = $("input, [type='text']");

Please see:
http://api.jquery.com/attribute-equals-selector[^],
http://api.jquery.com/multiple-selector[^],
http://api.jquery.com/element-selector/[^].

When this is done, you can use the .each() loop and traverse the set. To tell the empty values from non-empty, first trim the value, then check up resulting length:
var totalCount = 0;
allTextBoxes.each(function() {
   var textBox = $(this); // I'll go slowly, step by step
   var value = textBox.val(); // it also removes carriage return characters
   if (value.trim().length > 0) // exclude the empty ones
      ++totalCount;
});

Please see:
http://api.jquery.com/val[^],
http://www.w3schools.com/jsref/jsref_trim_string.asp[^].

Note, this form of .each(), https://api.jquery.com/each[^], should no be confused with jQuery.each(array, callback), which also can be used, but is much less useful in this case.

It is possible that you have some text boxed on the same page which are not in the table in question, so you need to exclude it. Then you need to modify the selector using the same principle I demonstrated in the very beginning. You may also want to eliminate the input attribute check-up, if you know that all the inputs in the table are of the same type. Here is one idea: https://api.jquery.com/child-selector[^].

And so on…

—SA
   
v6
Comments
Maciej Los 19-Mar-15 14:38pm
   
Sergey, you always surprises me ;)
Sergey Alexandrovich Kryukov 19-Mar-15 14:53pm
   
"Ко всему привыкает человек, и Герасим привык к городскому житью". :-)
("A man can get used to anything, so Gerasim get used to the life in city".
A quote from the story "Mumu" after Ivan Sergeyevich Turgenev.)
Thank you, Maciej.
—SA
Maciej Los 19-Mar-15 16:18pm
   
I'd translate it to Polish: "Do wszystkiego ludzie przywykają i Gierasim przywykł do miejskiego życia".
I found online version: http://www.online-literature.com/turgenev/1972/ and bookmarked it. I'll read it in (near?) future.
Sergey Alexandrovich Kryukov 19-Mar-15 19:46pm
   
Not that I specifically recommended you to read it, it's just what was always learned by all school students, along with "Fathers and Children". Turgenev is a great writer, but not my very favorite.
—SA
sudevsu 19-Mar-15 14:47pm
   
5ed
Sergey Alexandrovich Kryukov 19-Mar-15 14:48pm
   
Thank you very much.
Are you sure it's a "5"? I can see one vote of 1 and one of 5. Not a problem, but it could be a mistake of one of the two voters.
—SA
sudevsu 19-Mar-15 15:20pm
   
Yeah That's me SA. I wanted to click on 5th star but somehow got clicked at 1. That's the reason, I wrote a message again with 5. :)
Maciej Los 19-Mar-15 16:11pm
   
Refresh the page and re-vote ;)
Sergey Alexandrovich Kryukov 19-Mar-15 19:40pm
   
No problem and thanks.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 19 Mar 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100