Select a dropdown value automatically using a value from the URL
This is really handy for selecting a specific insurance package when it is supplied in the querystring (URL) from another website. For instance, you may have an existing website which lists all the insurance packages you sell but want to have a single Scheme on the SchemeServe side which is selected automatically from a dropdown box. The following code does that for you.
Just change the values 'package' and 'DropDownID' to be the names of your querystring parameter and dropdown box ID respectively.
Then drop this code as normal into your Custom Javascript area.
//
// EXAMPLE CODE FOR SELECTING DROPDOWN VALUE FROM QUERYSTRING ON GET QUOTE PAGE
// the following code uses neither jQuery nor Prototype
//
//
// get a value from the URL querystring
function get_param(param) {
var search = window.location.search.substring(1);
var compareKeyValuePair = function(pair) {
var key_value = pair.split('=');
var decodedKey = decodeURIComponent(key_value[0]);
var decodedValue = decodeURIComponent(key_value[1]);
if (decodedKey == param) return decodedValue;
return null;
};
var comparisonResult = null;
if (search.indexOf('&') > -1) {
var params = search.split('&');
for (var i = 0; i < params.length; i++) {
comparisonResult = compareKeyValuePair(params[i]);
if (comparisonResult !== null) {
break;
}
}
} else {
comparisonResult = compareKeyValuePair(search);
}
return comparisonResult;
}
// selects the chosen value from the specified dropdown box
function selectDropDownItem(desiredValue, dropdownName) {
var el = document.getElementById(dropdownName);
if (el) {
for (var i = 0; i < el.options.length; i++) {
if (el.options[i].text == desiredValue) {
el.selectedIndex = i;
break;
}
}
}
}
// do nothing unless the page is loaded
if (document.loaded) {
customJavascript();
} else {
if (window.addEventListener) {
window.addEventListener('load', customJavascript, false);
} else {
window.attachEvent('onload', customJavascript);
}
}
// run different custom functions on different pages
function customJavascript() {
try {
var title = document.title;
if (title.indexOf('Quotation Request') != -1) {
function1();
} else if (title.indexOf('Administration') != -1) {
function2();
} else if (title.indexOf('Viewing Summary') != -1) {
function3();
} else if (title.indexOf('Client details') != -1) {
function4();
} else {}
} catch (err) {
txt = "There was an error in the custom Javascript.\n\n";
txt += "Error description: " + err.description + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
}
function function1() {
// get the value passed in for package in the querystring
var param_value = get_param('package');
// select the package from the dropdown list
selectDropDownItem(param_value, 'DropDownID');
}
Please sign in to leave a comment.
Comments
0 comments