Solved

JavaScript conditional checkboxes

Posted on 2011-03-22
4
192 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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