Counting the number of selected checkboxes in a form

Hi,

I have a form setup, all checkboxes and I want to count the total number of checkboxes selected, update a record that the form is based on, then I'll print that number on another page. 

I could use Javascript, though my scripting skills are many years old. I do have a script in place that simply adds up the checkboxes and alerts the total on screen, just to check it at a basic level. It's a bit messy...

$(document).on('knack-form-submit.view_543', function() {
  var checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
  if (checkboxes.length <=9){
  alert('Your workplace stress score is low at '+checkboxes.length+' out of 36.');
  }
  if (checkboxes.length > 9 && checkboxes.length <=18){
  alert('Your workplace stress score is moderate at '+checkboxes.length+' out of 36.');
  }
  if (checkboxes.length > 18 && checkboxes.length <=27){
  alert('Your workplace stress score is high at '+checkboxes.length+' out of 36.');
  }
  if (checkboxes.length > 27 && checkboxes.length <=36){
  alert('Your workplace stress score is very high at '+checkboxes.length+' out of 36.');
  }
  alert (checkboxes.length);
return checkboxes.length;
});

Do you guys have examples of updating a record after a form submit? The developer docs talk about the structure of the code, but an example would really help. The dev docs show this:

// Change view_1 to the form view you want to listen to

$(document).on('knack-form-submit.view_1', function(event, view, record) {

alert('Form submitted!'); });

I'd actually prefer to find a non-javascript way of totalling up how many checkboxes are selected in an entire form (I don't need it broken down per question). They tell you to check an object ID by right clicking on the record table checkbox area and looking to see the ID there. But if I'm submitting the form, that's not practical. The ID will only *just* have been created by the form submission. I wondered about formula fields, but they only allow you to count from related records that have a numerical value.

Any thoughts? I'm stumped.

Hi Susanne

Glad you've got there!

I thought I would make one suggestion - maybe a bit late - but then I wondered whether you did this anyway...

Anyway - in this case you could use a single lookup table with a second field which identified which lookup field to connect it to - you could then filter the values listed for each.

Julian

Hi guys,

I got this working by (eventually) creating lookup fields for each section of the quiz. These got totalled up in my workplace stress object, then a final total field to sum it together. Not ideal and a bit messy, but I got there.

Thanks again,

Susanne.

Kelson...thank you. I'm in the UK and will get in touch tomorrow morning when I look into this further.

Julian...thank you also. I have text options. Each checkbox simply needs to represent a score of one.

The exercise(form) uses the 'workplace stress' object, which currently links with an employee object and an HR object. One employee to many workplace stresses, one workplace stress to one employee and similarly with HR. 

I'll tinker tomorrow and let you know how I get on.![](upload://qpT7ng5wuxaiTp2rwslO0ZwcTyt.png)

You don't mention whether your checkboxes are checkboxes for Multiple Choice Fields - or yes/no fields displayed as checkboxes?

If the latter then you can simply add them up in an equation and Yes = 1 and No = 0 - simple.

If it's a multiple choice field, you can also use the values in an equation if you have a number in the option values - this isn't that great because you have to display a number. For this reason I would use a many to many connection to a lookup table which has the text for the checkboxes - and use the Format option of Checkboxes:

![](upload://1jnPi02jzOUWqCuEcKabnwG86fw.png)

You can then use a Count field to count how many options have been chosen.

These approaches us Knacks built in database features - my motto is to use the database wherever possible.

Hi Susanne, 

I'll lend a hand and help you with this. Reach out and we can jump on a call: kelson@ksensetech.com

Thanks Kelson, I understand the general concept. Not sure which listener to use and how to implement though. Is it like this one?

$(document).on('knack-scene-render.scene_1', function(event, scene) {

// Do something after the scene renders

alert('listener for scene: ' + scene.key);

});

Then how would the event be referred to? And is the seven referred to like knack-scene-render-scene_100 (if the scene number is 100 for example).

 

Hi Susanne, 

What you are going to want to do is add a number field at the bottom of all of the checkboxes. Then what you can do is on the page render add an event listener for anytime a checkbox is changed. When there is a change you can count the checkboxes and put the number into the number field. 

Does that make sense?