Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-14-2002, 04:58 PM   PM User | #1
Shabina
New to the CF scene

 
Join Date: Jun 2002
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Shabina is an unknown quantity at this point
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.
Shabina is offline   Reply With Quote
Old 06-14-2002, 06:39 PM   PM User | #2
JohnKrutsch
Regular Coder

 
Join Date: Jun 2002
Location: The Planet Earth Code Poet: True
Posts: 282
Thanks: 0
Thanked 1 Time in 1 Post
JohnKrutsch is an unknown quantity at this point
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>
JohnKrutsch is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:06 AM.


Advertisement
Log in to turn off these ads.