Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Dec 14, 2015 Dec 14, 2015

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.

1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 14, 2015 Dec 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){

    

...
Translate
LEGEND ,
Dec 14, 2015 Dec 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,

^_^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 14, 2015 Dec 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);

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2015 Dec 14, 2015
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources