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

x
?
Solved

JavaScript conditional checkboxes

Posted on 2011-03-22
4
Medium Priority
?
199 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 1000 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.
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