3 Replies Latest reply on Dec 14, 2015 1:42 PM by pirlo89

    If checkbox is checked value="0" if is not checked value="1"?

    pirlo89

      Hello all,

      I'm trying to get my checkbox functionality to work depends on if check box is checked or not. If I click on the checkbox and is Checked I should send value="0" with my Ajax call. In other case if I unchecked my checkbox I should send value="1" with my Ajax. Here is the code that I have so far:

       

      <td><input type="checkbox" name="block" id="block" onChange="updateTable('#CustomerID#')"></td>

       

      function updateTable(CustomerID){

           if(document.getElementById('block').checked == true){

                 var blocked = 0;

                 var whObj = {"url":"/apps/StudentAdmin/ParentTeacherConfScheduler/PTCAjaxFunction.cfc?method=UpdateSc hedule&returnformat=json"};

                 gwAjaxPost(whObj,{"CustomerID":CustomerID,"blocked":block},Result);

           }else if(document.getElementById('block').checked == false){

                var unblocked = 1;

                var whObj = {"url":"/apps/StudentAdmin/ParentTeacherConfScheduler/PTCAjaxFunction.cfc?method=UpdateSc hedule&returnformat=json"};

                gwAjaxPost(whObj,{"CustomerID":CustomerID,"unblocked":block},Result);

          }

      }

       

      This code above always submit value 1. Does not matter if I checked or unchecked the checkbox. I'm not sure why, if anyone see where is my code breaking or if you know any other way how I can send different values please let me know.

        • 1. Re: If checkbox is checked value="0" if is not checked value="1"?
          WolfShade Level 4

          The first thing I'd do is replace your if/else with a switch/case; a little easier to read, IMHO, and it only runs the correct code.

           

          It looks like you're setting two different variables, depending upon the if/else, and using neither of them.  At least that's what it looks like, to me.  You're setting variable "blocked" in one case, "unblocked" in another, and sending "block" in the ajax.

           

          Since "block" isn't being set in the function, I assume it is being set somewhere else - otherwise, it would error.

           

          HTH,

           

          ^_^

          • 2. Re: If checkbox is checked value="0" if is not checked value="1"?
            BKBK Adobe Community Professional & MVP

            Is it your intention to do this instead, that is,pass the arguments CustomerID and block to the CFC.

             

            function updateTable(CustomerID){

                 if(document.getElementById('block').checked == true){

                       var blocked = 0;

                       var whObj = {"url":"/apps/StudentAdmin/ParentTeacherConfScheduler/PTCAjaxFunction.cfc?method=UpdateSc hedule&returnformat=json"};

                       gwAjaxPost(whObj,{CustomerID:CustomerID,block:blocked},Result);

                 }else if(document.getElementById('block').checked == false){

                      var unblocked = 1;

                      var whObj = {"url":"/apps/StudentAdmin/ParentTeacherConfScheduler/PTCAjaxFunction.cfc?method=UpdateSc hedule&returnformat=json"};

                      gwAjaxPost(whObj,{CustomerID:CustomerID,block:unblocked},Result);

                }

            }

            • 3. Re: If checkbox is checked value="0" if is not checked value="1"?
              pirlo89 Level 1

              Yes that was the problem in my code. Now I got this to work. Thank you.