Solved

Javascript Checkbox oncheck uncheck other checkboxes

Posted on 2012-03-12
4
372 Views
Last Modified: 2012-03-12
Hello,

I have a form, with three checkboxes on it -

<table width="200" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><input <%If (CStr((MemberDetail.Fields.Item("UK-Member").Value)) = CStr("Y")) Then Response.Write("checked=""checked""") : Response.Write("")%> name="uk" type="checkbox" id="uk" value="Y" /></td>
            <td class="chechtext">UK Member</td>
          </tr>
          <tr>
            <td><input <%If (CStr((MemberDetail.Fields.Item("Eire-Member").Value)) = CStr("Y")) Then Response.Write("checked=""checked""") : Response.Write("")%> name="Eire" type="checkbox" id="Eire" value="Y" /></td>
            <td class="chechtext">Eire Member</td>
          </tr>
          <tr>
            <td><input <%If (CStr((MemberDetail.Fields.Item("EU-Member").Value)) = CStr("Y")) Then Response.Write("checked=""checked""") : Response.Write("")%> name="EU" type="checkbox" id="EU" value="Y" /></td>
            <td class="chechtext">EU Member</td>
          </tr>
        </table>

Open in new window


I'd like to write a javascript snippet that if uk is selected (Checked) deselects any of the other two options that maybe selected. So that at any point in time only one of the checkboxes can be checked..

In essence whichever checkbox is checked, the other two aren't..

Appreciate any assistance with this -

Thanks
0
Comment
Question by:garethtnash
4 Comments
 
LVL 15

Assisted Solution

by:StingRaY
StingRaY earned 167 total points
Comment Utility
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 166 total points
Comment Utility
Hi garethtnash,

For this type of functionality, you can use Radio buttons.
Is there any specific reason for using checkboxes, because radio buttons can solves this problem without writing so much code?
0
 
LVL 18

Accepted Solution

by:
nap0leon earned 167 total points
Comment Utility
Simple answer is to change them to radio buttons.

If you need to use checkboxes, then here is one way you can do it.  Add an "onclick" event to each checkbox that calls a function.  This function unchecks the checkboxes and then re-checks the one that called the function.
<table width="200" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td><input name="uk" type="checkbox" id="uk" value="Y" onclick="cbxChange(this);"/></td>
		<td class="chechtext">UK Member</td>
	</tr>
	<tr>
		<td><input name="Eire" type="checkbox" id="Eire" value="Y" onclick="cbxChange(this);"/></td>
		<td class="chechtext">Eire Member</td>
	</tr>
	<tr>
		<td><input name="EU" type="checkbox" id="EU" value="Y" onclick="cbxChange(this);"/></td>
		<td class="chechtext">EU Member</td>
	</tr>
</table>
<script>
function cbxChange(cbx){
	document.getElementById('uk').checked = false;
	document.getElementById('Eire').checked = false;
	document.getElementById('EU').checked = false;

	document.getElementById(cbx.id).checked=true;
}
</script>

Open in new window

0
 

Author Closing Comment

by:garethtnash
Comment Utility
Thank you all :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

11 Experts available now in Live!

Get 1:1 Help Now