Last active
June 6, 2025 10:56
-
-
Save saifsultanc/68c96c97ce139bcda6996e954b9b593a to your computer and use it in GitHub Desktop.
count-number-of-characters.js
This file contains hidden or 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
// 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