Javascript build not working on checkboxlist

I have a dynamically created table that can have radiobutton list and / or checkbox list

In the following code the .rblsurvey works perfectly and populates hdnsurveyvalues

However
the .chks does not populate hdnsurveyvaluesChk properly I have verified the names etc

Following is the code and the screen print

    <asp:HiddenField ID="hdnsurveyvalues" runat="server" />	
     <asp:HiddenField ID="hdnsurveyvaluesChk" runat="server" />	
 <script>
            function SaveValidation() {
                var surveydata="";		
                $('.rblsurvey').each(function () {		
                    debugger;		
                    var checked_radio = $("[id*="+$(this).context.id+"] input:checked");		
                    var value = checked_radio.val();		
                    var question = $(this).context.className.replace("rblsurvey rblMultiList_","");		
                    surveydata=surveydata+question+","+value+"|";		
                    //alert(surveydata);		
                    $("#ctl00_ContentPlaceHolder1_hdnsurveyvalues").val(surveydata);		
                });

                $(".chks").each(function (index) {
                    debugger;	
                    var questionid = $(this).attr("class").replace("chks chkquestions_", "");
                    var answerid = "";

                    $(this).find(":checked").each(function (index) {
                        answerid = answerid + $(this).val() + ",";
                    });
                    //  alert(questionid + " |" + answerid)                                        
                    $("#hdnsurveyvaluesChk").val($("#hdnsurveyvaluesChk").val() + questionid + "|" + answerid + ";");
                });
</script>

... bunch of htpm including survey...
<div style="float: left;">
    <asp:Button ID="btnAdd" runat="server" TabIndex="400" Text="Add Lead" class="btn btn-blue"
        OnClientClick="SaveValidation();" CssClass="btn-glow primary login" Style="height: 30px; width: 110px;" />
</div>

Open in new window


sp
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
Encapsulate the code (lines 16-26) into the
$('.rblsurvey').each(function () {		
   //code here		
                });

Open in new window

and retry
Larry Bristersr. DeveloperAuthor Commented:
Leonidas
I am a neophophite in JavaScript

I need a little more direction
Leonidas DosasCommented:
Irbister how do you want to show the checkBoxes?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Larry Bristersr. DeveloperAuthor Commented:
Leonidas
The checkboxes are showing
They build correctly and are on the page
I can check them etc...

My issue is that on .net btnAdd_Click it executes OnClientClick="SaveValidation();"

I need to iterate through the checkboxes that are checked... and build the string
Then set the hidden field value to that string

Idealy it would be
QuestionID, AnswerID, and a 0 or 1 for unchecked / checked then a |

And then next checkboxlist

I think I am close...
I think I need another var named  surveydatachk
It is just getting surveydatachk = surveydatachk+ questioned+","+answered=","+onoff+"|"
Leonidas DosasCommented:
Instead of
<asp:HiddenField ID="hdnsurveyvaluesChk" runat="server" />

Open in new window

set:
<asp:HiddenField ID="hdnsurveyvaluesChk" runat="server" Value="" />

Open in new window

and replace the line 25 with this:
$("#<%= hdnsurveyvaluesChk %>").val($("#<%= hdnsurveyvaluesChk %>").val() + questionid + "|" + answerid + ";");

Open in new window

Larry Bristersr. DeveloperAuthor Commented:
That did not work either
Julian HansenCommented:
If you do not give your checkboxes a value they default to value 'on' simply add value="1" for each checkbox and the 'on' in the message will be changed to a 1
Having said that - this is not a requirement for your example as you can get the same result by doing the following. Iterate over ALL checkboxes not just the checked ones and determine if they are checked or not - use that information to build your 1/0 string.
Example:
$(".chks").each(function (index) {
  debugger;  
  var questionid = $(this).attr("class").replace("chks chkquestions_", "");
  var answerid = "";

  $(this).find(":checkbox").each(function (i, e) {
    if (answerid != '') answerid += ',';
    answerid += e.checked ? 1 : 0;
  });

  $("#hdnsurveyvaluesChk").val($("#hdnsurveyvaluesChk").val() + questionid + "|" + answerid + ";");
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Julian
I will review shortly
Larry Bristersr. DeveloperAuthor Commented:
Thanks
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.