Javascript to set a checkbox to true by button click

  • 10 July 2018
  • 9 replies
  • 103 views

Badge +3

I need to create a button that will set a hidden checkbox field to true and then submit the form. I'm not a coder, but dally with javascript occasionally. I've searched everywhere for pre-existing code to do the task I am attempting, but can't find anything. I saw some posts about UNchecking radio buttons using javascript and I tried to modify it to make it work with the checkbox, but no go. Below is the code I tried:

NWF$('document').ready(function(){var strBeginReviewValue = NWF$('#'varBeginReviewID).find("input:checkbox:not(:checked)").val();NWF$('#'varBeginReviewID).find('input[value=''strBeginReviewValue'']').attr('checked', true);});

varBeginReviewID is the javascript ID for my checkbox. I placed this code in the client click field of the button. It does not set the checkbox to checked. The code above also does not submit the form. If anyone can help me get the button to do both the checking of the checkbox and submitting the form, it would be greatly appreciated!


Thanks!

Chris


9 replies

Userlevel 5
Badge +14

if you want the checkbox always checked, wouldn't it be easier to set its default value to checked?

you wouldn't need any javascript then

Badge +3

Hi Marian,

No, the use case is this:

The "Begin Review" checkbox is a trigger to start a review process workflow. Rather than have the user check a box and then click save, I want them to click a button which sets the Begin Review checkbox to true/yes, saves and closes the form and starts the workflow.

For some reason, you can set a button to send a value to single line of text, number and choice fields, but not to a checkbox. Seems like that ought to be a simple thing to do, so not sure why Nintex didn't include it as a field type to which you can send a value (true/false, 0/1, yes/no).

I have tried on a demo list and was able to make this work by changing Begin Review to a single line of text and using "Yes/No" as the values in the field. However, the list in production is in use and I don't want to have to change column types, so JavaScript to set the checkbox on button click is my best option. I was previously using InfoPath, so had no issues doing this at all. I have to upgrade all InfoPath solutions to Nintex Forms.

Userlevel 5
Badge +14

but that still sounds to me that you want/need to have it checked for each and every (new) item submitted, don't you?

since it is hidden, user doesn't have a control over it. and it shouldn't matter whether you set it checked from default value or from javascript. setting it from default value is much easier to implement and especially easier to maintain later on (nintex use to change controls' models almost with every release, so you have to be careful with upgrades and proof check all your custom js functionality).

or is there any further logic that I miss which controls when it should be checked and when not?

Badge +3

Hi Marian,

Thanks for your comments. The current order of operation is:

Standard User submits a form with information to the list. (Begin Review cannot be checked at this point)

Admin team receives notification that a submission has been received.

Admin team can look at submission without beginning review process. (Begin Review should not be checked at this point)

Admin team either checks the checkbox and saves the form to begin the review process or clicks a button to begin review process (latter is best option and either way, Begin Review should now be checked).

After Review process is begun, Review Team receives notification to review submission.

So the checkbox cannot be checked at all times as it is the trigger to begin the review process workflow.

Userlevel 5
Badge +14

that sounds to me to be already part of approval workflow then something that should checked in form.

but if you insist on checkbox approach try following

NWF$('#'+jsvarCheckBox).click();
Badge +3

Thanks. I will try this. I made a mistake in my previous comment. The checkbox is not the trigger to start a workflow, but a trigger to change state in the workflow state machine. The workflow starts on document upload.

Badge +3

I was not able to get this solution working as intended. I had to change the checkbox to a choice column with Yes or No as options. Then setting the button to change the Begin Review column to Yes was simple out of the box functionality.

Userlevel 5
Badge +14

how did you hide the checkbox? by its appearance settings?

place the checkbox into a panel and configure a formatting rule to hide the panel.

Badge +3

Ah, now I get it. I didn't have the checkbox on the form. I was trying to set the value for the checkbox to Yes in the list. Your solution works with the checkbox on the form.

Thanks!

Reply