Yes Chef Studio

Add query parameter value to hidden field

Pass through dynamic url values to your forms so you can track things like sales reps, traffic source, etc....

 <script> 

// Listen for when the HTML document has been completely loaded
document.addEventListener('DOMContentLoaded', function() {
    // Function to get a specific query parameter value from the URL
    function getQueryParamValue(paramName) {
        // Create an object to parse the URL query parameters
        const params = new URLSearchParams(window.location.search);
        // Return the value of the query parameter (e.g., 'salesRep')
        return params.get(paramName);
    }

    // Call the function to get the 'salesRep' query parameter value
    var queryValue = getQueryParamValue('your_query_parameter'); // change to your query parameter name

    // Check if the query parameter exists
    if (!queryValue) {
        // Exit if the parameter is not present in the URL
        return;
    }

    // Get all forms on the current web page
    var forms = document.querySelectorAll('form');
    
    // Loop through each form found
    forms.forEach(function(form) {
        // Find a specific hidden field within the form
        var hiddenField = form.querySelector('#your_field_id[type="hidden"]'); // Update selector as needed

        // If the hidden field is found, set its value
        if (hiddenField) {
            hiddenField.value = queryValue;
        }
    });
});


</script>