Solved

JavaScript conditional checkboxes

Posted on 2011-03-22
4
190 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
ID: 35191255
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
ID: 35191275
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
ID: 35191293
>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
ID: 35191313
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

785 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