Problem when to iterate through Radio Buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goodamr
    New Member
    • Jun 2007
    • 24

    Problem when to iterate through Radio Buttons

    Dear All,

    I'm building an Question/Answer Application in which I generate the Questions and Answers from Oracle database.

    I'd like to validate through javascript that the user should answer each question.

    First, I iterate through the questions.
    Then, inside each question, I check if the user has chosen an answer (If the radio button is checked). If not alert a message.

    I get the answer through:
    Code:
    var ans_rdo = document.getElementsByName('answer'+i);
    but, when I iterate through it to check if it's checked or not:
    Code:
    if(ans_rdo[j].checked == true)
    I get this error:
    Error: 'checked' is null or not an object

    Here is the complete function:
    ------------------

    Code:
    function ValidateEntery() 
    {
    	var qst_cnt = Number(document.Form1.quest_count.value);//Number of Questions
    	
    	for(i=1; i <= qst_cnt; i++)
    	{
    		var chk_cnt = 0;//number of radio checked
    		var ans_rdo = document.getElementsByName('answer'+i); //get the anser radio button
    		for(j=1; j <= ans_rdo.length; j++)
    		{
    			[B][U]if(ans_rdo[j].checked == true)[/U][/B] // here I get this error: 'checked' is null or not an object
    			{
    				chk_cnt ++;
    			}
    		}
    		
    		if(chk_cnt <= 0)
    		{
    			alert("Please answer all questions");
    			return false;
    		}
    		
    	}		
    }
    --------------------------
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the error implies that the fetched object is not a radio button (or checkbox). there might be another form element with that name.

    Comment

    • goodamr
      New Member
      • Jun 2007
      • 24

      #3
      Thank you Dormilich for replying.

      You are correct but the problem is because I have iterated through the radio buttons from 1:

      Code:
      for(j=1; j <= ans_rdo.length; j++)
      I should begin the iteration from 0 like that:

      Code:
      for(j=0; j < ans_rdo.length; j++)
      So the prblem has been solved.

      Comment

      Working...