Click here to Skip to main content
12,634,888 members (28,276 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript
I build a website that demonstrate finnacial website for investments. so I give to the clients option to answer questionnaire befor advice to them portfolio. the questionnaire is called from the database and shown by query. so to identify group of radiobuttons for checking which of them choosen in every question I gave different names to every group.(the name is @row.ID: 1 to the inputs of question 1, 2 to question 2.....)

but when I tryed to get the elements by function in javascript, it's not work!!! (I know becouse I tryed to write on screen the length of the "list" by document.write() but sometimes it's zero and sometimes show nothing!!!)

the HTML code is like that:




<form method="post" dir="rtl"> 
        <fieldset class="field"> 
          <legend style="direction: rtl"></legend> 
          <br>
          <div style="direction: rtl" id="questionList"> 
          @foreach(var row in db.Query(displayQuestion))
          { 
              <h3>/question number @counter: </h3>
              <h3>@row.subject: </h3>
              <h3>@row.describe</h3>
              <input type="radio" id="answer1" name="@row.ID" value="@row.ID"
                                                     önchange="chekRadioToAnswer(@row.ID,'1')" >@row.ans1<br>
              <input type="radio" id="answer2" name="@row.ID" value="@row.ID"
                                                     önchange="chekRadioToAnswer(@row.ID,'2')">@row.ans2<br>
              <input type="radio" id="answer3" name="@row.ID" value="@row.ID"
                                                     önchange="chekRadioToAnswer(@row.ID,'3')">@row.ans3<br>
              <input type="radio" id="answer4" name="@row.ID" value="@row.ID"
                                                     önchange="chekRadioToAnswer(@row.ID,'4')">@row.ans4<br>
              if(row.ans5 != null)
              {
                 <input type="radio" id="answer5" name="@row.ID" value="@row.ID" 
                                                     önchange="chekRadioToAnswer(@row.ID,'5')">@row.ans5<br>
              }
              <br>
              counter+=1;

          }
           <input type="hidden" id="hiddenAnswers" name="hiddenAnswers" value="0">

           <input type="hidden" id="hiddenAns1" name="hiddenAns1"  value="0">
           <input type="hidden" id="hiddenAns2" name="hiddenAns2" value="0">
           <input type="hidden" id="hiddenAns3" name="hiddenAns3" value="0">
           <input type="hidden" id="hiddenAns4" name="hiddenAns4" value="0">
           <input type="hidden" id="hiddenAns5" name="hiddenAns5" value="0">
           <input type="hidden" id="hiddenAns6" name="hiddenAns6" value="0">

           <br></br> 
            <input type="submit" value="submit questionnaire" class="submit" />
            <input type="button" style=" float: left" value="skip questionnaire"  önclick="ReDirect()" > 
           <br></br>  

         </div>
       </fieldset> 
</form> 


<script type="text/javascript">
var signAnswers = new Array(0, 0, 0, 0);
    var grades = [[0, 5, 10, 15], [15, 12, 10, 5, 0], [0, 5, 10, 15, 20], [0, 0, 20, 30]];
    var answers = new Array(0, 0, 0, 0, 0, 0);


    function chekRadioToAnswer(numQuestion, numAnswer) {
     if (numQuestion != 1 && numQuestion != 6) {
                var list = document.getElementByName(numQuestion); 
                for (i = 0; i < list.length; i++) {
                    if (list[i].value == numQuestion) {
                        if (list[i].checked) {
                            if (signAnswers[numQuestion - 2] == 0) {
                                var a = document.getElementById("hiddenAnswers").setAttribute("value", value + grades[numQuestion - 2][i]);
                                signAnswers[numQuestion - 2] = grades[numQuestion - 2][i];
                            }
                            else {
                                document.getElementById("hiddenAnswers").setAttribute("value", value - signAnswers[numQuestion - 2]);
                                document.getElementById("hiddenAnswers").setAttribute("value", value + grades[numQuestion - 2][i]);
                                signAnswers[numQuestion - 2] = grades[numQuestion - 2][i];
                            }
                            break;
                        }
                    }

                }
            }

        }
</script>
Posted 22-Nov-12 13:34pm
ofirhgy315

1 solution

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

Solution 1

it's getElementsByName not getElementByName. :-)
you have write getElementByName instead of getElementsByName
getElementByName is not a method you can check it using firebug.
you can see it,on console.
  Permalink  
v3

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 | Mobile
Web02 | 2.8.161208.2 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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