Subscription Center - Check/Uncheck All Javascript | Community
Skip to main content
January 23, 2017

Subscription Center - Check/Uncheck All Javascript

Recently someone on my team asked me for a bit of help in writing some JavaScript to check and uncheck the boxes on a subscription center when someone selects the Unsubscribe from All checkbox. You would think this code would be easy to find just floating around because so many people need it, but I actually couldn't find it all that easily. Since I'm probably not the only one who needs this code, why not share it?

Code to Check All Subscription Boxes

Make sure you update the field names in the quotes to match the field names in your instance, beginning with 'selectAll', which is the checkbox that, when selected, should populate all of the other checkboxes. Obviously, you should also remove any rows you don't need because you have less options than this example.

<script>

MktoForms2.whenReady(function (form) {

document.getElementById('selectAll').onclick = function() {

  if ( this.checked ) {

document.getElementById('mktosupplychainmanagement').checked = true;

document.getElementById('mktosmallmolecules').checked = true;

document.getElementById('mktobiologics').checked = true;

document.getElementById('mktoevents').checked = true;

document.getElementById('mktocommercialproductsupply').checked = true;

document.getElementById('mktoproductdevelopment').checked = true;

document.getElementById('mktogeneralmarketingcommunications').checked = true;

document.getElementById('Unsubscribed').checked = false;

  } else {

  // Did not Globally Subscribe

  }

  }

});

</script>

Code to Uncheck All Subscription Boxes

<script>

MktoForms2.whenReady(function (form) {

document.getElementById('Unsubscribed').onclick = function() {

  if ( this.checked ) {

document.getElementById('mktosupplychainmanagement').checked = false;

document.getElementById('mktosmallmolecules').checked = false;

document.getElementById('mktobiologics').checked = false;

document.getElementById('mktoevents').checked = false;

document.getElementById('mktocommercialproductsupply').checked = false;

document.getElementById('mktoproductdevelopment').checked = false;

document.getElementById('mktogeneralmarketingcommunications').checked = false;

document.getElementById('selectAll').checked = false;

  } else {

  // Did not Globally Unsubscribe

  }

  }

});

</script>

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

12 replies

January 30, 2017

Thank you @Sanford Whiteman​ as always for enhancing the code

Translate
SanfordWhiteman
Level 10
January 23, 2017

Browsers have built-in methods for selecting and iterating over elements. So this can be made tighter and easier to manage:

document.querySelector('#selectAll').onclick = function() {

  var dependentInputs = '#mktosupplychainmanagement,#mktosmallmolecules,#mktobiologics,#mktoevents';

 

  [].forEach.call(document.querySelectorAll(dependentInputs),function(input){

     input.checked = this.checked;

  }.bind(this));

}

This can also be wrapped up in a couple of dependency functions. I have some demos in other posts.

Translate