Solved

Javascript Checkbox oncheck uncheck other checkboxes

Posted on 2012-03-12
4
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

688 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