Code Snippets
Add query parameter value to hidden field
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>
Similar Code Snippets
Get class name from element
A nice little script that will return the first class applied to an element. It's part of our internal slider code, but has lots of uses when other js libraries are involved.
Fade non-hovered links
A fun animation where hovering over links will fade out all the items except the hovered item.
Fix side scrolling on all devices
The .page-wrapper is a common class used to wrap the entire content of a page. Sometimes, you might encounter elements that overflow the boundaries of your page, causing horizontal scrollbars or layout shifts. To address this, you can use the overflow: clip; property to prevent any content from overflowing the boundaries of the .page-wrapper: