Solved

Select one javascript for radio buttons

Posted on 2016-08-14
3
41 Views
Last Modified: 2016-08-14
I have the javascript below to make sure a user checks at least one checkbox.
Seems to work fine for checkboxes but not for radio buttons. I need help tweaking it so that it works.

I have a repeated region displaying names, they all have ids, I need the user to select one of the radio buttons.

<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type !="checkbox") continue;
if(form[f].checked){
return true;
}
}
alert("Select at least one User");{
return false;
}
}
</script>

Open in new window


A
0
Comment
Question by:amucinobluedot
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 41755471
Try this,
<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type =="radio") 
if(form[f].checked){
return true;
}
}
alert("Select at least one User");
return false;
}
</script>

<form id="form1">
<input type="radio" name="gender" value="male" > Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other

<input type="button" name="button" value="Click" onclick="oneCheckboxChecked(document.getElementById('form1'))"> <br>
</form>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41755498
Can I apply it to onsubmit for the form instead ?
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 41755510
Try this ,

<script language="JavaScript">
function oneCheckboxChecked(form){
for(f=0;f<form.length;f++){
if(form[f].type =="radio") 
if(form[f].checked){
return true;
}
}
alert("Select at least one User");
return false;
}
</script>

<form id="form1" onsubmit="return oneCheckboxChecked(this)">
<input type="radio" name="gender" value="male" > Male<br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other

<!--<input type="button" name="button" value="Click" onclick="oneCheckboxChecked(document.getElementById('form1'))">--> 
    
    <input type="submit" name="submit" value="Lets Submit"/> 
    <br>
</form>

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now