Skip to main content

Hi,

I have a form that I'm trying to use javascript on to convert the entries to title case so if someone enters a field in all caps or all lowercase i.e. JOHN SMITH, it'll convert it to John Smith.

This is the script I have but it doesn’t work:

<script>
// Get the form with ID "lp-pom-form-120"
const form = document.getElementById('lp-pom-form-120');

// Add an event listener to the form when it's submitted
form.addEventListener('submit', (event) => {
  // Loop through all form elements and find input elements
  form.querySelectorAll('input').forEach(input => {
    // Check if the input type is "text"
    if (input.type === 'text') {
      // Get the input value and convert to title case
      let inputValue = input.value.toLowerCase();
      inputValue = inputValue.split(' ').map(word => {
        return word.charAt(0).toUpperCase() + word.slice(1);
      }).join(' ');
      // Set the input value to the title case version
      input.value = inputValue;
    }
  });
});
</script>

Be the first to reply!

Reply