Jump to content

sorca_marian

Circle Member
  • Posts

    623
  • Joined

  • Last visited

Posts posted by sorca_marian

  1. If you use the latest Localized Forms, you can use the below code
     

    <script>
    
        /*
            In case you want to make immediate modifications on the form with the above methods.
            We need to check when the Squarespace React form was added
        */
    
        // 1. targetNode is the container or the form. If you have multiple forms, make sure to target the ".form-block" of that individual form.
        // 2. Inside the function startMyChangesOnTheForm(), put the code that makes changes on the form
        
        document.addEventListener("DOMContentLoaded", function(){
    
            var targetNode = document.querySelector('.form-block');
    
            // Options for the observer (which mutations to observe)
            var config = { childList: true, subtree: true };
    
            // Callback function to execute when mutations are observed
            var callback = (mutationList, observer) => {    
                mutationList.forEach(mutation => {
                    // debugger;
                    if( mutation.addedNodes.length ) {
                        mutation.addedNodes.forEach(addedNode => {
                            if(addedNode.classList.contains("react-form-contents")){
                                console.warn("Found react form mutation");
                                observer.disconnect();
                                startMyChangesOnTheForm();
                            }
                        });
                    }
                });
                
            };
    
            // Create an observer instance linked to the callback function
            const observer = new MutationObserver(callback);
            // Start observing the target node for configured mutations
            observer.observe(targetNode, config);
    
    
            // Here is where we can add our code that will make changes to the form
            function startMyChangesOnTheForm() {
                var form           = document.querySelector(".react-form-contents"),
                    firstNameInput = form.querySelector("[id*='fname-field']"),
                    lastNameInput  = form.querySelector("[id*='lname-field']");
    
    			firstNameInput.setAttribute("placeholder", "First Name");
                lastNameInput.setAttribute("placeholder", "Last Name");
    
            }
    
        });
    </script>

    This code is based on this code https://github.com/sorcamarian/squarespace-tricks/blob/main/Forms/modify-localized-form-input-with-javascript.html

    I have a video about it in case you are interested

     

  2. Hi,

    It's probably from a Squarespace update.

    Until they fix it, you can try the below code in the "page header code injection". 

    <style>
    .summary-block-collection-type-blog-basic-grid img {
        height: 100%!important;
        width: 100%!important;
        aspect-ratio: 1 / 1;
        object-fit: cover;
        position: static!important;
    }
    
    .summary-block-collection-type-blog-basic-grid .summary-thumbnail {
        padding-bottom : 0!important;
    }
    </style>

     

    Screenshot 2023-12-18 at 20.15.13.png

    Screenshot 2023-12-18 at 20.15.36.png

  3. The code might run before the react form is implemented.

    Try using the below code.
    It checks when the react form was added on the page.
     

    document.addEventListener("DOMContentLoaded", function(){
    
        var targetNode = document.querySelector('.form-block');
    
        // Options for the observer (which mutations to observe)
        var config = { childList: true, subtree: true };
    
        // Callback function to execute when mutations are observed
        var callback = (mutationList, observer) => {    
            mutationList.forEach(mutation => {
                // debugger;
                if( mutation.addedNodes.length ) {
                    mutation.addedNodes.forEach(addedNode => {
                        if(addedNode.classList.contains("react-form-contents")){
                            console.warn("Found react form mutation");
                            observer.disconnect();
                            startMyChangesOnTheForm();
                        }
                    });
                }
            });
            
        };
    
        // Create an observer instance linked to the callback function
        const observer = new MutationObserver(callback);
        // Start observing the target node for configured mutations
        observer.observe(targetNode, config);
    
    
    
        function startMyChangesOnTheForm() {
            // Do the programmatical canges on the form
        }
    
    });

    1. targetNode is the container or the form. If you have multiple forms, make sure to target the ".form-block" of that individual form.
    2. Inside the function startMyChangesOnTheForm(), put the code that makes changes on the values of the form

     

  4. Hello @NovaSites,

    I'm glad you like my content. Thank you for recommending it.

    Have you tried to add normal form fields to the form, hide them with CSS, and fill those fields with JavaScript?

    I have a code to hide the fields publicly but show them in the control panel: https://github.com/sorcamarian/squarespace-tricks/blob/main/Style/only-visible-in-control-panel.css

    And here is the video 

     

  5. If somebody wants to run some JavaScript after form submission for example for conversion tracking you can check this video: 

    Here are the source codes: https://github.com/sorcamarian/squarespace-tricks/tree/main/Forms

    You can track form changes using regular DOM events and have form data stored.
    After the above code for form submission is executed you can get the latest data stored and do HTTP requests with it.

  6. On 5/16/2023 at 4:37 PM, silllli said:

    This may work now (thank you for pointing it out!), but cries to break anytime something changes in React or the form again. I’d rather have Squarespace to provide a way of submitting custom form data.

    I agree.

    I am waiting for a response from Squarespace technical team about this and if I get a official way of doing this I will post it here.

  7. 14 hours ago, creedon said:

    SS has indeed updated the form block and so custom code has broken.

    SS is using React for their forms now.

    I'm am not familiar with React but I found this possible rough gem.

    function changeValue(input,value){
    
        var nativeInputValueSetter = Object.getOwnPropertyDescriptor(
          window.HTMLInputElement.prototype,
          "value"
        ).set;
        nativeInputValueSetter.call(input, value);
    
        var inputEvent = new Event("input", { bubbles: true });
        input.dispatchEvent(inputEvent);
    }

    Source

    Basically React is swallowing up programmatic changes to inputs and this code may help with that.

    I did a quick test and it seems to work but I make no promises if it will work for you or continue to work.

    Let us know how it goes.

    @sorca_marian

    Thank you so much! It works!

×
×
  • 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.