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
Solved

Javascript Checkbox oncheck uncheck other checkboxes

Posted on 2012-03-12
4
382 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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