Skip to content

Instantly share code, notes, and snippets.

@adroste
Created April 28, 2023 15:52
Show Gist options
  • Save adroste/29772807dbb15b78671199e7a5da960d to your computer and use it in GitHub Desktop.
Save adroste/29772807dbb15b78671199e7a5da960d to your computer and use it in GitHub Desktop.
Salesforce Pardot Dependent Field on Checkbox
<script type="text/javascript">
function dependentCheckboxField(checkboxName, dependentFieldName) {
function normalizeString(str) {
return str.toLocaleLowerCase().replace(/\s/g, '').trim();
}
function findPardotFormFieldByLabel(label) {
for (const labelEl of document.querySelectorAll('#pardot-form label')) {
if (normalizeString(labelEl.innerHTML + '') === normalizeString(label))
return labelEl.parentElement;
}
}
const checkboxEl = findPardotFormFieldByLabel(checkboxName);
const dependentFieldEl = findPardotFormFieldByLabel(dependentFieldName);
function showDependentField(show) {
if (show) dependentFieldEl.style.removeProperty('display');
else dependentFieldEl.style.display = 'none';
}
checkboxEl.addEventListener('change', function (event) {
let checkedCount = parseInt(dependentFieldEl.dataset.checkedCount) || 0;
checkedCount = event.target.checked ? checkedCount + 1 : checkedCount - 1;
dependentFieldEl.dataset.checkedCount = checkedCount;
showDependentField(checkedCount > 0);
});
showDependentField(
checkboxEl.querySelector('input[type="checkbox"]').checked
);
}
dependentCheckboxField('My first checkbox','E-Mail');
dependentCheckboxField('My first checkbox','Name');
dependentCheckboxField('My second checkbox','Address');
dependentCheckboxField('My second checkbox','E-Mail');
</script>
@adroste
Copy link
Author

adroste commented Apr 28, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment