Solved

CHECKBOX STATE JAVASCRIPT FUNCTION

Posted on 2004-10-04
4
6,254 Views
Last Modified: 2008-01-09
Hi Experts,

I am trying to produce a function which checks to see whether a checkbox within a form is either checked or unchecked. The function is to be called from the checkbox itself so when the checkbox is clicked the function works out whether the checkbox has just been checked or unchecked. WHat I have written so far is as follows:

<script language="javascript">
<!--
function travelIns() {
            frmVal = document.detailsForm
            if (frmVal.travelInsurance.checked)
            {
            document.write("it's checked")
            }
            if (!frmVal.travelInsurance.checked)
            {
            document.write("it's not checked")
            }
}
//-->
</script>


and the checkbox code is as follows:

<form action="confirmDetails.asp" method="post" name="detailsForm" onSubmit="return checkDetails()">
<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns();" checked>
</form>


It's not behaving as I expected. The function always returns "it's not checked" - any ideas?




PJORDANNA




0
Comment
Question by:pjordanna
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 12217147

<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns(this.checked);" checked />



function travelIns(checked) {
  if ( checked )
     alert("checked");
  else
     alert("not checked");
}

0
 
LVL 5

Expert Comment

by:apparition
ID: 12217148
<script language="javascript">
<!--
function travelIns() {
          frmVal = document.detailsForm
          if (frmVal.travelInsurance.checked)
          {
          alert("it's checked")
          }
          if (!frmVal.travelInsurance.checked)
          {
          alert("it's not checked")
          }
}
//-->
</script>
</head>

<body>





<form action="confirmDetails.asp" method="post" name="detailsForm" onSubmit="return checkDetails()">
<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns();" checked>
</form>
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12217185
Two things:

You're writing to the document after it's already been established.  This could be part of the problem.  As the two people above mentioned, you should alert it instead.  Like this:

function travelIns() {
    frmVal = document.detailsForm;
    if (frmVal.travelInsurance.checked) {
        alert("it's checked");
    } else {
        alert("it's not checked");
    }
}

The second point I want to make is that you first test if it's checked, then again test if it's not checked, with a second if.  This can be accomplished with one if statement, using the else clause.

Hope this helps.
0
 

Expert Comment

by:sreekanth_k77
ID: 12346167
<HTML>
<HEAD>
<TITLE>Test Checkbox and Radio button </TITLE>
<SCRIPT LANGUAGE="javascript">
 function showHide(sStatus)
 {
    //alert(document.frmTest.chkShow1.checked)
      if (sStatus == 1)
      {
          if ( document.frmTest.chkShow1.checked)
               alert("Yeh, first checkbox is checked")
          else
               alert("Ooopps, first checkbox is unchecked")
      }
      else
      {
                   if ( document.frmTest.chkShow2.checked)
             alert("Yeh, Second checkbox is checked")
        else
             alert("Ooopps, Second checkbox is unchecked")
      }
 }
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME = "frmTest">
   <INPUT TYPE = "checkbox" NAME = "chkShow1" onClick = "showHide(1)" >  Show Div area  <br>
   <INPUT TYPE = "checkbox" NAME = "chkShow2" onClick = "showHide(2)" >  Hide Div area  <br>
</FORM>
</BODY>
</HTML>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disabled form text field 2 22
Why does my button disappear? 3 26
Elegant Way to Include Query String When it exists? 4 42
Having problem getting this to work 2 17
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

932 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

14 Experts available now in Live!

Get 1:1 Help Now