Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing text field array to javascript function

    Hi there, newish to javascript so grateful for any help.

    I am creating a test generator and have an array of textfields ("answer[]") created by a simple piece of PHP code. As you can see, I have radio buttons corresponding to each text field, which is a potential answer to a multi choice question. I already have working code which ensures that an answer is selected and not the default option. However, I also want to validate that when an answer is selected, the corresponding text field is not blank. I am having problems accessing the values of each text field in the array.

    Here's the php/html code....

    <p>
    Enter the answer options and ensure the radio button selected is the correct answer. <br/><br/>
    <?php
    //Generate form
    $count=5;
    $name=array("A","B","C","D","E");
    for ($i=0;$i<$count;$i++) {
    echo "<input type='radio' name='option' value='$i'>$name[$i] <input type='text' name='answer[$i]' size='80'><br/>";
    }
    ?>
    <input type="radio" name="option" value="No Answer" checked="checked" />No Answer selected

    and here's the JS function to check it.....

    function validateCorrectAnswer(form){
    var error="";
    var none="No Answer";
    var ansText ="";
    var selected;

    for (var i=0; i<form.option.length; i++) {
    if (form.option[i].checked)
    {
    selected = form.option[i].value ;
    if (selected==none)
    error = "You didn't select a correct answer.\n";
    else if(form.elements['answer[i]'].value=="")
    error = "You didn't enter any text for the correct
    answer.\n";
    }
    }
    return error;
    }

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function validateCorrectAnswer(form){
      var error="";
      var none="No Answer";
      var ansText ="";
      var selected;
    
      for (var i=0; i<form.option.length; i++) {
        if (form.option[i].checked)
        {
          selected = form.option[i].value ;
          if (selected==none)
             error = "You didn't select a correct answer.\n";
          else if (form.elements['answer[' + i + ']'].value=="")
             error = "You didn't enter any text for the correct answer.\n";
        }
      }
      return error;
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •