Jump to content

Remove last name field in newsletter form only and change first name field to full name

Go to solution Solved by MuhammadIsa,

Recommended Posts

Posted

Hi,

I am looking to remove the last name field for my newsletter blocks only as I have seen in other forum posts but I would also like to change the first name field to full name.

How can this be done?

Kind regards,

Muhammad Isa

  • Solution
Posted

Hey,

I have a solution for this now:

Add to Code Injection --> Footer

<script>
document.addEventListener('DOMContentLoaded', function() {
  const newsletterForm = document.querySelector('.newsletter-form-body');
  const lnames = document.querySelectorAll('.last-name input');
  const lnameFields = document.querySelectorAll('.last-name');

  if (newsletterForm) {
    // Create the full name input field
    const fullNameInput = document.createElement('input');
    fullNameInput.className = 'newsletter-form-field-element field-element field-control';
    fullNameInput.name = 'fullname';
    fullNameInput.type = 'text';
    fullNameInput.spellcheck = 'false';
    fullNameInput.placeholder = 'Full Name';
    fullNameInput.maxLength = 60;
    fullNameInput.required = true;

    // Create the wrapper div for the full name input
    const fullNameWrapper = document.createElement('div');
    fullNameWrapper.className = 'newsletter-form-field-wrapper form-item fields name required';
    fullNameWrapper.style.verticalAlign = 'bottom';

    // Create the label for the full name input
    const fullNameLabel = document.createElement('label');
    fullNameLabel.className = 'newsletter-form-field-label title';
    fullNameLabel.textContent = 'Full Name';

    // Append the label and input to the wrapper div
    fullNameWrapper.appendChild(fullNameLabel);
    fullNameWrapper.appendChild(fullNameInput);

    // Find the existing first name and last name fields
    const firstNameField = newsletterForm.querySelector('.newsletter-form-field-wrapper.field.first-name');
    const lastNameField = newsletterForm.querySelector('.newsletter-form-field-wrapper.field.last-name');

    // Replace the first name field with the full name field
    if (firstNameField) {
      firstNameField.parentNode.replaceChild(fullNameWrapper, firstNameField);
    }

    // Hide the last name field and set its default value
    lnames.forEach(lname => {
      lname.value = "-";
    });
    lnameFields.forEach(lnameField => {
      lnameField.style.display = "none";
    });

    // Handle form submission
    newsletterForm.addEventListener('submit', function(event) {
      const fullName = fullNameInput.value.trim();

      if (!fullName) {
        event.preventDefault();
        alert('Please enter your full name.');
        return;
      }

      // Split the full name into first name and last name
      const nameParts = fullName.split(' ');
      const firstName = nameParts[0];
      const lastName = nameParts.slice(1).join(' ') || '-';

      // Create hidden inputs for first name and last name if not already present
      let firstNameHiddenInput = newsletterForm.querySelector('input[name="fname"]');
      let lastNameHiddenInput = newsletterForm.querySelector('input[name="lname"]');

      if (!firstNameHiddenInput) {
        firstNameHiddenInput = document.createElement('input');
        firstNameHiddenInput.type = 'hidden';
        firstNameHiddenInput.name = 'fname';
        newsletterForm.appendChild(firstNameHiddenInput);
      }

      if (!lastNameHiddenInput) {
        lastNameHiddenInput = document.createElement('input');
        lastNameHiddenInput.type = 'hidden';
        lastNameHiddenInput.name = 'lname';
        lastNameHiddenInput.value = '-';  // Set a default value for last name
        newsletterForm.appendChild(lastNameHiddenInput);
      }

      firstNameHiddenInput.value = firstName;
      lastNameHiddenInput.value = lastName;
    });
  }
});
</script>

 

  • 1 month later...
Posted
On 8/21/2024 at 4:41 PM, Gass said:

Hello everyone,

Thank you in advance for your help.

I would like something similar. Please modify my contact form on my Home page:

  • Ensure that the 'Name' field displays only one line of text (without 'First Name' / 'Last Name').
  • Ensure that the 'Address' field also displays only one line of text.

Thank you!

Cheers, 

Gatien

https://mayflower-leopard-cmnt.squarespace.com/config/pages

Can you provide password?

image.png.c403d8d9e92bbb3bb819992ba79d7192.png

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.