Skip to content

Instantly share code, notes, and snippets.

@saifsultanc
Last active June 6, 2025 10:56
Show Gist options
  • Save saifsultanc/68c96c97ce139bcda6996e954b9b593a to your computer and use it in GitHub Desktop.
Save saifsultanc/68c96c97ce139bcda6996e954b9b593a to your computer and use it in GitHub Desktop.
count-number-of-characters.js
// Customize field IDs and offset
const fieldIds = [1, 2, 3, 4]; // Text fields
const targetFieldId = 5; // Number field to hold total count.
const offset = -1000; // Offset value (Can be positive or negative)
const getInputSelector = (id) => `#input_${GFFORMID}_${id}`;
const targetSelector = getInputSelector(targetFieldId);
function updateCharacterCount() {
let total = 0;
fieldIds.forEach((id) => {
const field = document.querySelector(getInputSelector(id));
if (field) {
total += field.value.length;
}
});
total += offset;
const targetField = document.querySelector(targetSelector);
if (targetField) {
targetField.value = total;
targetField.dispatchEvent(new Event('change'));
}
}
fieldIds.forEach((id) => {
const field = document.querySelector(getInputSelector(id));
if (field) {
field.addEventListener('input', updateCharacterCount);
}
});
updateCharacterCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment