Solved

Javascript Checkbox oncheck uncheck other checkboxes

Posted on 2012-03-12
4
377 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
ID: 37709746
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 166 total points
ID: 37709751
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
ID: 37709768
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
ID: 37709792
Thank you all :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

914 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

12 Experts available now in Live!

Get 1:1 Help Now