Query Param to Hidden Field
A JavaScript utility to fetch a query parameter from the URL and set it as the value of a hidden form field. There are two versions of this utility: one designed for backward compatibility including support for Internet Explorer, and a modern version without Internet Explorer support.
- Fetches a specified query parameter from the URL.
- Sets the fetched value to a specified hidden form field.
- If the query parameter is not found, it sets "Query parameter not found" as the value of the hidden field.
- Uses
dataattributes for configuration, making it easy to adapt for various form setups.
- Clone or download this repository.
- Depending on the version you need:
- For backward compatibility: Copy the
formHandler.jsfile to your project directory. - For modern browsers only: Copy the
formHandlerModern.jsfile to your project directory.
- For backward compatibility: Copy the
-
In your HTML form, set the following
dataattributes on the<form>element:data-query-param-name: The name of the query parameter you want to fetch.data-hidden-field-id: The ID of the hidden field you want to set the value to.
Example:
<form data-query-param-name="desired QueryParamName" data-hidden-field-id="desiredHiddenFieldId"><input type="hidden" id="desiredHiddenFieldId" name="desiredHiddenFieldName" value=""> <!-- rest of the form --> </form>
-
Include the chosen script file at the end of your HTML:
<!-- For backward compatibility version --> <script src="path/to/your/formHandler.js"></script> <!-- For modern browsers version --> <script src="path/to/your/formHandlerModern.js"></script>
-
That's it! The script will automatically fetch the specified query parameter and set its value to the hidden field when the page loads.
- Designed to work with older browsers, including Internet Explorer.
- Optimized for modern browsers.
- Does not support Internet Explorer.
Contributions are welcome! Please open an issue or submit a pull request.
This project is open source and available under the MIT License.