Web Analytics Made Easy -
StatCounter How do i run a function ONLY when a checkbox is UNCHECKED? - CodingForum

Announcement

Collapse
No announcement yet.

How do i run a function ONLY when a checkbox is UNCHECKED?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do i run a function ONLY when a checkbox is UNCHECKED?

    i've been using the onClick handler but that runs the function every time the checkbox is checked.

    i have a checkbox that is initially checked, and if the user unchecks it i want to run a function, but if they check it again, i don't want to run the function.

    in other words, i ONLY want to run the function when the box is UNCHECKED.

    thanks.
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #2
    You can check whether a checkbox is checked by using the checked attribute of the form element.

    In your case, the event handler should be written like the following:

    onclick="if(!this.checked)DoSomething();"

    Comment


    • #3
      thanks poccil, but when i try to recheck the checkbox, it doesn't let me. is there a way to fix this?

      thanks,
      Ed Ventura
      Graphic/Web Designer
      www.blackwatercompany.com

      Comment


      • #4
        can we see the code you're using?
        bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

        i am a loser geek, crazy with an evil streak,
        yes i do believe there is a violent thing inside of me.

        Comment


        • #5
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <script language="JavaScript">
          function appendHidden(ckvalue) {
          var str = document.form;
          var ckvalue;

          str.hiddenfield.value = str.hiddenfield.value + '-' + ckvalue;

          alert(str.hiddenfield.value);
          return false;
          }
          </script>
          </head>
          <body bgcolor="white">
          <form name="form" method="post" action="javascript:;">
          <input type="hidden" name="hiddenfield" value="">
          <p>
          <input type="checkbox" name="box1" value="box1unchecked" checked onClick="if (!this.checked) { appendHidden(this.value);} else { return false;}">
          box1<br>
          <input type="checkbox" name="box2" value="box2unchecked">
          box2<br>
          <input type="checkbox" name="box3" value="box3unchecked" checked onClick="appendHidden(this.value);">
          box3<br>
          <input type="checkbox" name="box4" value="box3unchecked">
          box4<br>
          <input type="submit" name="Submit" value="Submit">
          </p>
          </form>
          </body>
          </html>
          Ed Ventura
          Graphic/Web Designer
          www.blackwatercompany.com

          Comment


          • #6
            <input type="checkbox" name="box1" value="box1unchecked" checked onClick="if (!this.checked) { appendHidden(this.value);}else { return false;}">

            i think you need to get rid of the bit of bold. let me know if that does the trick.
            bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

            i am a loser geek, crazy with an evil streak,
            yes i do believe there is a violent thing inside of me.

            Comment

            Working...
            X