Solved

JavaScript conditional checkboxes

Posted on 2011-03-22
4
184 Views
Last Modified: 2012-05-11
I have the following code attached. It is almost perfect, however, I get some incorrect value.
If I click on Video only, it is fine. If I then click on Email as well, it is fine. If I then click off of video, I get an incorrect value (I get Nothing).

If I click off both values, it is correct. If I click on Email, it is correct, If I click on Video as well, it is correct. If I click off of email, I get an incorrect value (I get Nothing).

I am testing this via an alert.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function $(id) { return document.getElementById(id); }
</script>
</head>
<body>
<form action="" method="get">

<legend>Button Names:</legend>

<input id="checkboxA" type="checkbox" value="ChoiceA" onclick="if(this.checked && $('checkboxB').checked) $('button').name='Both';else if(this.checked) $('button').name='Video';else if($('checkboxA').checked)$('button').name='Video';else $('button').name='Nothing'" />
<label for="checkboxA">Video</label>
<br />
<input id="checkboxB" type="checkbox" value="ChoiceB" onclick="if(this.checked && $('checkboxA').checked) $('button').name='Both';else if(this.checked) $('button').name='Email';else if($('checkboxB').checked)$('button').name='Email';else  $('button').name='Nothing'" />
<label for="checkboxB">Email</label>
<br />

<input type="button" value="Original button text" id="button" name="Nothing" onclick="alert($('button').name)">

<br />
</form>
</body>
</html>

Open in new window

0
Comment
Question by:msukow
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
Change lines 15 & 18.

Switch the id names: 'checkboxA' and 'checkboxB'

See below...

 
<input id="checkboxA" type="checkbox" value="ChoiceA" onclick="if(this.checked && $('checkboxB').checked) $('button').name='Both';else if(this.checked) $('button').name='Video';else if($('checkboxB').checked)$('button').name='Video';else $('button').name='Nothing'" />

<input id="checkboxB" type="checkbox" value="ChoiceB" onclick="if(this.checked && $('checkboxA').checked) $('button').name='Both';else if(this.checked) $('button').name='Email';else if($('checkboxA').checked)$('button').name='Email';else  $('button').name='Nothing'" />

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
check the last test :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function $(id) { return document.getElementById(id); }
</script>
</head>
<body>
<form action="" method="get">

<legend>Button Names:</legend>

<input id="checkboxA" type="checkbox" value="ChoiceA" onclick="if(this.checked && $('checkboxB').checked) $('button').name='Both';else if(this.checked) $('button').name='Video';else if($('checkboxB').checked)$('button').name='Email';else $('button').name='Nothing'" />
<label for="checkboxA">Video</label>
<br />
<input id="checkboxB" type="checkbox" value="ChoiceB" onclick="if(this.checked && $('checkboxA').checked) $('button').name='Both';else if(this.checked) $('button').name='Email';else if($('checkboxA').checked)$('button').name='Video';else  $('button').name='Nothing'" />
<label for="checkboxB">Email</label>
<br />

<input type="button" value="Original button text" id="button" name="Nothing" onclick="alert($('button').name)">

<br />
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
Comment Utility
>Switch the id names: 'checkboxA' and 'checkboxB'

the new name too :
.name='Video' -> .name='Email'
.name='Email' -> .name='Video'
0
 
LVL 4

Assisted Solution

by:GeoffHarper
GeoffHarper earned 250 total points
Comment Utility
Sorry, also switch the trace values...


<input id="checkboxA" type="checkbox" value="ChoiceA" onclick="if(this.checked && $('checkboxB').checked) $('button').name='Both';else if(this.checked) $('button').name='Video';else if($('checkboxB').checked)$('button').name='Email';else $('button').name='Nothing'" />

<input id="checkboxB" type="checkbox" value="ChoiceB" onclick="if(this.checked && $('checkboxA').checked) $('button').name='Both';else if(this.checked) $('button').name='Email';else if($('checkboxA').checked)$('button').name='Video';else  $('button').name='Nothing'" />

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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.

744 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

14 Experts available now in Live!

Get 1:1 Help Now