Solved

CheckBox Value - Unchecked?

Posted on 2003-11-04
5
7,468 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
  • 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: 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

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…
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?
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…

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