Jump to content

Code to default state/country and hide country no longer work

Recommended Posts

Since May 2020, I had used the code below in "Code Injection" to default the State and Country fields, and then hide the Country fields for all of the forms on my site.  But it seems that Squarespace had an update a few months ago that broke this.   I'd like to know what I can do (code changes) to get that functionality back.  Below is the code I used and source.

<!--added by MOAKES 5/24/20 from https://forum.squarespace.com/topic/141931-autofill-country-field-in-form-on-index-page/#comment-183491 --> 
 <script>
     Y.on("domready", function(){
     Y.all('input[name="state"]').setAttribute("value", "GA");
     Y.all('input[name="country"]').setAttribute("value", "USA");  
     });
</script>
<style>
   .field.country {
      display: none;
   }
</style>
<!--  End MOAKES -->

 

Link to comment
  • Replies 5
  • Views 2.8k
  • Created
  • Last Reply

Top Posters In This Topic

  • 4 months later...

@momo2000,

Not sure if you ever got this fixed, but would this work?

<script>
    document.addEventListener("DOMContentLoaded", function () {
        // Set default value for the state input based on aria-label
        var stateInput = document.querySelector('input[aria-label="State"]');
        if (stateInput) {
            stateInput.value = "GA";
        }

        // Set default value for the country select based on aria-label
        var countrySelect = document.querySelector('select[aria-label="Country"]');
        if (countrySelect) {
            countrySelect.value = "USA";
        }
    });
</script>

<style>
    .field.country {
        display: none;
    }
</style>

@SeptemberDesign, this might not work for you as your site could be set up differently.

Link to comment
  • 3 months later...

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.