Created
April 28, 2023 15:52
-
-
Save adroste/29772807dbb15b78671199e7a5da960d to your computer and use it in GitHub Desktop.
Salesforce Pardot Dependent Field on Checkbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://return2.net/salesforce-pardot-dependent-field-on-checkbox/