Skip to main content
Known Participant
December 14, 2015
Answered

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

  • December 14, 2015
  • 2 replies
  • 1591 views

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=UpdateSchedule&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=UpdateSchedule&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.

This topic has been closed for replies.
Correct answer BKBK

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=UpdateSchedule&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=UpdateSchedule&returnformat=json"};

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

    }

}

2 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
December 14, 2015

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=UpdateSchedule&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=UpdateSchedule&returnformat=json"};

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

    }

}

pirlo89Author
Known Participant
December 14, 2015

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

WolfShade
Legend
December 14, 2015

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,

^_^