# Hubpsot Form clearing check boxes when leveraging JavaScript

**URL:** <https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484>\
**Category:** CMS Development\
**Created:** [October 5, 2018, 3:45pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484 "2018-10-05T15:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben\_groneman](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben_groneman/32/78471_2.png) [@ben\_groneman](https://community.hubspot.com/u/ben_groneman)\
**Post date:** [October 5, 2018, 3:45pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/1 "2018-10-05T15:45:25Z")

</div>

I am currently attempting to leverage JS to map clicks on a ‘pseudo’ checkbox to the actual form checkboxes so that the data sent from the form can still be added to our workflow, and maintain contact properties for the user. A partial code snippet is shown below.

```auto
/**
 * Handles the click event and delegates what state the checkboxes should be in
 * @author - Ben G.
 */
function handleClickEvent(isChecked, element) {switch(isChecked) {
 case true:
 element.prop("checked", true).change();
 break;
 case false:
 element.prop("checked", false).change();
 break;
 case null:
 console.log("Something is awry the value of checked is: " + isChecked);
 }

```

I am using the .change() function in order to make the change on the HubSpot side of the form. However, the issue that I am running into is that once the user goes to fill out the form, and they move onto a different form field, the checkboxes are immediately cleared. This behavior doesn’t occur when the user actually clicks the checkboxes, so this has left me to be rather stumped.

Any help regarding this issue will be greatly appreciated, thank you!

---

<div class="post-metadata">

**Author:** ![tjoyce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tjoyce/32/88832_2.png) [@tjoyce](https://community.hubspot.com/u/tjoyce)\
**Post date:** [October 5, 2018, 5:55pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/2 "2018-10-05T17:55:53Z")

</div>

[@ben\_groneman](https://community.hubspot.com/t5/user/viewprofilepage/user-id/72592) - Is this an embed code? or the form module in the Design Manager?

If it’s a form module, you should use the global form events to wrap your code after the form has loaded on the page [Accounts Dashboard | HubSpot](https://developers.hubspot.com/global-form-events)

If it’s an embed code, use the onFormReady callback function in the embed code.

* * *

If this answer helped, please, mark as solved 😄

* * *

[tim@belch.io](mailto:tim@belch.io) | [forms.belch.io](https://forms.belch.io) | Design your own Beautiful HubSpot Forms; No coding necessary.

Drop by and say Hi to me [on slack](http://slack.belch.io/).

---

<div class="post-metadata">

**Author:** ![ben\_groneman](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben_groneman/32/78471_2.png) [@ben\_groneman](https://community.hubspot.com/u/ben_groneman)\
**Post date:** [October 5, 2018, 7:25pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/3 "2018-10-05T19:25:16Z")

</div>

tjoyce,

Thanks for the speedy reply!

This is a form module in the Design Manager. I think that global from events is a step in the right direction. Thank you for sending this my way.

by ‘wrap your code’ do you essentially mean that the JS that I have written should use the eventListener mentioned ([in the Hubspot Documentation](https://developers.hubspot.com/global-form-events)) to instantiate the functions that I have created? I am currently using the following code to wait until the entire DOM has loaded to enqueue my functions.

```auto
window.onload = function() {
 setRealInputClasses();
 setPseudoInputClasses();
 initializeEventListeners();
};

```

I have a link shown below that will show you the exact issue that I am experiencing. Please let me know if you have any questions, if we work together I think we could very well solve this issue. Thank you!

[Video of Issue](https://drive.google.com/file/d/16wQdWOs-Rdts8OO1_dir8OYgBAjTOjIb/view?usp=sharing)

---

<div class="post-metadata">

**Author:** ![tjoyce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tjoyce/32/88832_2.png) [@tjoyce](https://community.hubspot.com/u/tjoyce)\
**Post date:** [October 5, 2018, 7:31pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/4 "2018-10-05T19:31:28Z")

</div>

[@ben\_groneman](https://community.hubspot.com/t5/user/viewprofilepage/user-id/72592) - Yes, you’re right, call your function from the onFormready. Doing the window.onload won’t quite cut it because the window isn’t aware of the form that’s pulling in through the ajax call.

---

<div class="post-metadata">

**Author:** ![ben\_groneman](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben_groneman/32/78471_2.png) [@ben\_groneman](https://community.hubspot.com/u/ben_groneman)\
**Post date:** [October 5, 2018, 8:39pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/5 "2018-10-05T20:39:25Z")

</div>

My apologies,

I don’t believe that I am understanding what you mean by this. I have tried the following blocks of code, with no avail.

```auto
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') { setRealInputClasses(); setPseudoInputClasses(); initializeEventListeners();
 console.log("Form Submitted!");
 }
});

```

```auto
hbspt.forms.create({ 
 portalId: 'xxxxx',
 formId: 'aa8b5b4a-62ac-461b-a387-XXXXXXXXXXX',
 onFormReady($form, ctx){
 setRealInputClasses();
 setPseudoInputClasses();
 initializeEventListeners();
 	}
 });

```

The first code block I attempted did not go through because of the if statement would loop a few times, and continually return a false value.

The second code block would not register because ‘forms’ was undefined.

I am not sure where the disconnect here might be, but I feel as if I am not fully grasping why the form would be resetting the checkboxes based on a users event.

---

<div class="post-metadata">

**Author:** ![tjoyce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tjoyce/32/88832_2.png) [@tjoyce](https://community.hubspot.com/u/tjoyce)\
**Post date:** [October 8, 2018, 12:44pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/6 "2018-10-08T12:44:03Z")

</div>

[@ben\_groneman](https://community.hubspot.com/t5/user/viewprofilepage/user-id/72592) - I’m not entirely sure what the root cause is but, you’re trigger(‘change’) is either not firing properly, or not bubbling propperly. If I change your code to

```auto
	switch (b)
	{
	case true:
		a.click();
		break;
	case false:
		a.prop("checked", false).change();
		break;
	case null:
		console.log("Something is awry the value of checked is: " + b)

```

simulating a click is working fine, but changing the prop manually does not. Maybe this will help you in debugging.

---

<div class="post-metadata">

**Author:** ![ben\_groneman](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben_groneman/32/78471_2.png) [@ben\_groneman](https://community.hubspot.com/u/ben_groneman)\
**Post date:** [October 9, 2018, 3:13pm UTC](https://community.hubspot.com/t/hubpsot-form-clearing-check-boxes-when-leveraging-javascript/10484/7 "2018-10-09T15:13:40Z")

</div>

tjoyce,

Thank you so much for helping me debug this issue. Upon changing the action to be a click, and changing the window.onload function, I was able to solve this issue.

Your help is greatly appreciated. I hope that others can benefit from this posting as well!

Best wishes,

~ B
