html - JavaScript document.getElementsByName() argument to match input name as array -


i using php generate form questions:

<form action="formaction.php" method="post" name="form">  <?php ($j=0;$j<$no_of_ques;$j++) switch ($qtype[$j]) {          case shorttext:         echo " <textarea name='qno[$j]'></textarea>";         break;    case checkbox:    for($l=0;$l<$no_of_choices;$l++)          { echo "<input type='checkbox' name='qno[$j]' value='$choices[$l]'>$choices[$l]";         }         break; } ?> <a href='#' class='submit button'onclick='myfunction()'>submit</a> <script>  function myfunction(){  if (document.getelementsbyname("qno").checked)      document.forms['form'].submit();       }     </script> 

this incorrect.i using styled anchor submit form js. how must use js validate if each question has been answered?

give them same classname , access them via getelementsbyclassname. way can keep name attribute that.

<form action="formaction.php" method="post" name="form">  <?php ($j=0;$j<$no_of_ques;$j++) switch ($qtype[$j]) {          case shorttext:         echo " <textarea class="xxx" name='qno[$j]'></textarea>";         break;    case checkbox:    for($l=0;$l<$no_of_choices;$l++)          { echo "<input class="xxx" type='checkbox' name='qno[$j]' value='$choices[$l]'>$choices[$l]";         }         break; } ?> <a href='#' class='submit button'onclick='myfunction()'>submit</a> <script>  function myfunction(){       var ok = true;      for(obj in document.getelementsbyclassname("xxx")) {          if(!obj.checked) {              ok = false;              break;          }      }      if(ok) {         document.form.submit();      } } </script> 

sorry on phone , cant test :)


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -