Solved

JavaScript conditional checkboxes

Posted on 2011-03-22
4
194 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
[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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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