Solved

CheckBox Value - Unchecked?

Posted on 2003-11-04
5
7,436 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now