?
Solved

CheckBox Value - Unchecked?

Posted on 2003-11-04
5
Medium Priority
?
7,498 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 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

801 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