[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript Checkbox oncheck uncheck other checkboxes

Posted on 2012-03-12
4
Medium Priority
?
404 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 668 total points
ID: 37709746
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 664 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 668 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

656 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