Javascript function to populate from query string
function populateFieldsFromQueryString() {
// Get the query string from the URL
const queryString = window.location.search;
// Check if there is a query string
if (queryString) {
// Remove the leading "?" character
const queryParams = queryString.substring(1);
// Split the query string into key-value pairs
const keyValuePairs = queryParams.split("&");
// Loop through the key-value pairs
keyValuePairs.forEach((pair) => {
// Split each pair into key and value
const [key, value] = pair.split("=");
// Decode the key and value (in case of URL encoding)
const decodedKey = decodeURIComponent(key);
const decodedValue = decodeURIComponent(value);
// Find the corresponding field in the document
const field = document.querySelector(`[name="${decodedKey}"]`);
// Check if the field exists
if (field) {
// Populate the field with the value
field.value = decodedValue;
}
});
}
}
Comments
Post a Comment