Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In asp.net i am using javascript for getting the input values and that part completed if i considered only single value... Now i am counting values from database in that i am having 100 records for this purpose i require loop here. I am getting values only the last record what is issue with for loop in javascript.

What I have tried:

var count = total; <--- this total is calculated in code behind
for(var i=1;i<=count;i++)
alert(count);
In count i am getting 100th record. But i require from first to hundred records how can i achieve this
Posted
Updated 19-Dec-18 6:54am

See here: Javascript for loop and alert - Stack Overflow[^] - Paulpro's answer.
 
Share this answer
 
Comments
Member 8583441 19-Dec-18 12:34pm    
If i give total.length then even 100th record is not getting means totally value is null
Afzaal Ahmad Zeeshan 19-Dec-18 12:56pm    
total might be an integer value, thus length is not available there. You need to study the JavaScript type system a bit, and see what types are available and which functions are available on each type.
Member 8583441 19-Dec-18 23:48pm    
thanks very much sir you saved my time. Actually the variable total is called as string but in method to get the value i have initialized as integer that's the wrong i have done. Now i changed it to string and getting 100 records as well when giving total.length to var i.
Member 8583441 19-Dec-18 23:54pm    
Now i am getting another error regarding the selecting the input value. Actually this for loop is used for getting the input values and this loop is initialized in input on change in jquery that is working as expected but my problem was i am getting null value for this
let selectedOption = $('input[name=options' + i + ']:checked', '#form1').val();
I have 4 radio buttons these four radio buttons getting the value as expected but in these 4 radio buttons we will select one radio button that value i required but it is returning null. how can i solve this issue and second problem was i have html label's. I need to get the text value of these labels. I am getting for it but 1st label text i am getting at 3rd label and so on... how to solve this issue also
let lblTextA = document.getElementById('lblChoiceA' + i).innerHTML;
At the first glance I thought you were experiencing issues created by the var in JavaScript and was going to suggest the usage of let. :laugh:

But after reading the question complete, I came to know that perhaps you need to do this,
for(var i = 1; i <= count; i++) {
   alert(i);
}
And that also answers the problem you are facing,
Quote:
In count i am getting 100th record
That is because count is set to be 100 (in your code behind).

Also, you are not even accessing any element from anywhere—collection[i]—so how can you say that you are getting 100th? Did you mean to say, you get 100 always?

Please see the following links,
scope - What's the difference between using "let" and "var" to declare a variable in JavaScript? - Stack Overflow[^]
let | MDN[^]
for | MDN[^]

You can also try to explore the loop in this online fiddle that I created, Edit fiddle - JSFiddle[^]
 
Share this answer
 
v2
Comments
Member 8583441 20-Dec-18 1:28am    
can you tell me sir how to use collection[i] in for loop
Afzaal Ahmad Zeeshan 20-Dec-18 3:45am    
That was an example of the way to access elements from array, it had nothing to do with your specific case.
Member 8583441 20-Dec-18 4:13am    
yes sir you're right nothing to give collection[i] here. I just changed i<=count.length to i<=count and removed var and taken let it is working as expected means if there is 100 records then 100 times this loop should happen. Thanks very much for giving me the solution that to change var to let
Member 8583441 20-Dec-18 6:26am    
i want these values store in asp.net hidden field using javascript or jquery but i am getting only last value. how to solve this issue i am struggling since afternoon. Please tell me sir

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900