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

Thread: Function

  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Function



    <form> tag contains <input type= radio> in a loop (ie) if a page contain many questions with radio buttons as options, and if any question left unchecked it should alert me a dialog box saying not clicked. If there is only one question in a page by wrinting a javascript code, it can be checked.



    What javascript code should be implemented as soon as the submit button of the page is clicked, checking all the radio buttons be clicked and if one radio button is unchecked pop a dialog box.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    I am sure there are lots of ways to accomplish this but something along these lines ought to do it:

    Code:
    <script type="text/javascript">
    <!--//
    function radioCheck(){
      var temp=bad="", ok=0;
      for(var i=0; i<document.f.elements.length; i++){
        if(document.f.elements[i].type=="radio"){
          if(temp!=document.f.elements[i].name || i==document.f.elements.length-2){
            if(ok==0 && temp!="") bad +="  "+temp+"\n";
              temp=document.f.elements[i].name;
              ok=0;        
          }
          ok+=document.f.elements[i].checked;
        }
      }
      
      if (bad!=""){
        alert("Be sure to choose one of the following:\n\n"+bad);
        return false;
      }
      else{
        return true;
      }
    
    }
    //-->
    </script>
    
    <form name="f" onsubmit="return radioCheck()">
      gender:
      <input type="radio" name="gender" value="male"> male
      <input type="radio" name="gender" value="female"> female
      <br />
      I like pizza:
      <input type="radio" name="pizza" value="true"> true
      <input type="radio" name="pizza" value="false"> false
      <br />
      I like chocolate:
      <input type="radio" name="chocolate" value="true"> true
      <input type="radio" name="chocolate" value="false"> false
      <br />
      <input type="submit" value="submit">
    </form>


  •  

    Posting Permissions

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