Detect checkbox clicked

brycefraser
Giga Contributor

Hey all

I have a catalogue form with a large list of checkboxes.

I'd like to know how to detect which checkbox has been clicked by a requestor so I can apply a style to differentiate it from the checkboxes that are auto-filled.

I've no idea where to start.


Anyone got any ideas?

Thanks!

1 ACCEPTED SOLUTION

Hi Bryce, you were right! Here goes the fix. Add the following at the beginning of your inline defined onclick function (@ line 12)



gel('IO:' + item.sys_id).value = this.checked;if (typeof(variableOnChange) == 'function') variableOnChange('IO:' + item.sys_id);




I'll appreciate if you can mark this post as the correct one or as a helpful one so that others reading this post in the future know that this line is *key* to be added in the inline defined onclick function.




Thanks,


Berny


View solution in original post

23 REPLIES 23

Hi Berny,



Thank you very much for the information, it was exactly what I was looking for.



Here is the block of code I used;




function onLoad() {


    //Type appropriate comment here, and begin script below



  var target = new GlideRecord('item_option_new');


  target.addQuery('variable_set', 'SYS ID OF VARIABLE SET HERE');


  target.query();


  while (target.next()){


            if (target.type == '7'){


                      var item_sysid = target.sys_id;


                      gel('ni.IO:' + item_sysid).onclick = function(){


                                alert('This is checked: ' + this.checked + ' and its value is ' + this.value);


                      };


            };


  };


}


Hi Bryce, give a try to the following. It makes the GlideRecord asynchronous and you shall see a far better loading time of your catalog item:




function onLoad() {  


  //Type appropriate comment here, and begin script below  


 


  var target = new GlideRecord('item_option_new');  


  target.addQuery('variable_set', 'SYS ID OF VARIABLE SET HERE');  


  target.query(processResponse);  


 


  function processResponse(target){


          while (target.next()){  


                if (target.type == '7'){  


                      var item_sysid = target.sys_id;  


                      gel('ni.IO:' + item_sysid).onclick = function(){  


                              alert('This is checked: ' + this.checked + ' and its value is ' + this.value);  


                      };  


                }  


          }


  }  


}





Thanks,


Berny


Bernie,



Thanks again for your advice!



I am having an issue now, once the form is submitted, any of the checkboxes that were checked don't filter down to the requested item record.



Is this because I am changing the onclick function? If so, what should I be adding to this function to allow it to work properly?



Thanks!


Bryce Fraser


Hi Bryce, nop. I don't think that adding the custom onclick function would have break your code. When i tried this on my fuji instance the oob onclick function do not got impacted and the one I have inline defined was running as expected as well.



Would you mind sharing a little bit of the code to filter down to the requested item to assess what could be wrong? Where is it defined, in an onchange script?



Thanks,


Berny


Hey Bernie



Here is a direct paste of my code. Sorry for the format, this text box is really hard to get code into.


function onLoad() {


    //Type appropriate comment here, and begin script below




  var target = new GlideRecord('item_option_new');


  target.addQuery('variable_set', '2d18189c288e4200582e4db8471a13ab');


  target.query();


  while (target.next()){


  if (target.type == '7'){


  var item_sysid = target.sys_id;


                      gel('ni.IO:' + item_sysid).onclick = function(){


  // get sys id from io object



  var nam = this.name.split(":");


  var nam = nam[1];



  // get name from sys id


  var fieldtarget = new GlideRecord('item_option_new');


  fieldtarget.addQuery('sys_id',nam);


  fieldtarget.query();


  while (fieldtarget.next()){


  var targetname = fieldtarget.name;


  }



  if (this.checked == true){


  g_form.getControl(targetname).parentNode.style.color = 'red';


  g_form.getControl(targetname).parentNode.style.fontWeight = 'bold';


  } else{


  g_form.getControl(targetname).parentNode.style.color = 'black';


  g_form.getControl(targetname).parentNode.style.fontWeight = 'normal';


  };




                      };


            };


  };


}