Solved

CheckBox Value - Unchecked?

Posted on 2003-11-04
5
7,488 Views
Last Modified: 2008-03-06
I am trying to get an element to change when a checkbox is checked or unchedcked. I am not sure how to read or get the value of an unchecked checkbox. I can get the results I want when the checkbox is checked, but not unchecked. When the checkbox is unchecked, nothing happens.

Here is the code I am currently playing with:


  <html>
  <head>
  <title>Test Document</title>
 
   <script language="JavaScript" defer="true">
     
       function TestCKBox()
       {
        if(TestForm.TheCheckBox.value == "True")
        {
         TestDataVal.style.display = "";
        }
        else
        {
         TestDataVal.style.display = "none";
        }
       }

   </script>
  <body>

  <form name="TestForm">
  <input type="checkbox" name="TheCheckBox" value="True" onclick="JavaScript:TestCKBox()">The CheckBox
  </form>
  <div id="TestDataVal" style="display:none;">TEST</div>

  </body>
  </html>
0
Comment
Question by:swinslow
[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
5 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 150 total points
ID: 9681656
Use checkbox.checked attribute ...

       function TestCKBox()
       {
        if(TestForm.TheCheckBox.checked)
        {
         document.getElementById("TestDataVal").style.display = "block";
        }
        else
        {
         document.getElementById("TestDataVal").style.display = "none";
        }
       }

also use document.getElementById instead of directly accessing the div element.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9681669
<html>
 <head>
 <title>Test Document</title>
 
  <script language="JavaScript" defer="true">
       function TestCKBox(fObj,theDiv){
            var el = document.getElementById(theDiv);
        if(fObj.checked == true){
                  el.style.display = "";
            } else {
                  el.style.display = "none";
            }
      }
  </script>
 <body>

 <form name="TestForm">
 <input type="checkbox" name="TheCheckBox" value="True" onclick="TestCKBox(this,'TestDataVal')">The CheckBox
 </form>
 <div id="TestDataVal" style="display:none;">TEST</div>

 </body>
 </html>
0
 
LVL 4

Author Comment

by:swinslow
ID: 9681727
amit_g - Thank you very much! That works perfect.

Soren

0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9681760
swinslow

It does not work in Netscape as mine does.

you will need to modify it to work for NS

0
 
LVL 4

Author Comment

by:swinslow
ID: 9682687
Doesnt matter much about Netscape in this application. It for an intranet web site, and Netscape has been permanently banned for a couple years.

Thanks anyway. I will remember that if I need it for one of our internet sites.

Soren

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

691 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