Discussions
Categories
Setting the value of a hidden field to the display text value from a drop list
We use a picklist for our country drop downs in forms where the value matches the country code in SFDC. It's a ugly string of characters that gives you no clue what the country value is when you see a notification email.
I am personally not great with javascript, but I've managed to get the country display value added to a hidden field so it is sent in the notification email with lots of googling. To save you from googling here is the javascript:
//set value of hidden field from drop down list display textfunction ddlselect () {//set d to the drop down list var d = document.getElementById("fe44703");//grab the display text of the selected item var displaytext=d.options[d.selectedIndex].text;//set the value of the hidden fielddocument.getElementById("fe44715").value=displaytext;}window.onload = function() {document.getElementById("fe44703").addEventListener("change", ddlselect);}
In your form HTML you will be able to find the id values for your drop down list and hidden field. In this example my drop down list is fe44703 and my hidden field is fe44715. (Note: getElementByName is deprecated in HTML5).
Comments
-
My real life application needs to both capture the country display name from the drop down, but also populate other hidden values from query string parameters. Here is the final javascript for both.
var defaultHiddenFieldNameValue = "";
function getQueryStringParamValue(strQStrParam) {
var strURL = document.location.href;
var strQStrParamValue = "";
if (strURL.indexOf('?') != -1)
{
strQStrParamValue = strURL.substr(strURL.indexOf('?') + 1);
if (strQStrParamValue.indexOf(strQStrParam) != -1)
{
strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf(strQStrParam));
strQStrParamValue = strQStrParamValue.substr(strQStrParamValue.indexOf('=') + 1);
if (strQStrParamValue.indexOf('&') != -1)
strQStrParamValue = strQStrParamValue.substr(0, strQStrParamValue.indexOf('&'));
return strQStrParamValue;
}else{
strQStrParamValue = defaultHiddenFieldNameValue;
return strQStrParamValue;
}
}else{
strQStrParamValue = defaultHiddenFieldNameValue;
return strQStrParamValue;
}
}
// Form name goes here
var form = "BP_CiB_EN";
function setCampaign(){
var elqForm = document.forms[0];
//repeat for each field to populate
elqForm.elements['CiB'].value = getQueryStringParamValue('CiBName');
elqForm.elements['rurl'].value = getQueryStringParamValue('rurl');
}
//set value of hidden field from drop down list display text
function ddlselect () {
//set d to the drop down list
var d = document.getElementById("fe44703");
//grab the display text of the selected item
var displaytext=d.options[d.selectedIndex].text;
//set the value of the hidden field
document.getElementById("fe44715").value=displaytext;
}
window.onload = function() {
document.getElementById("fe44703").addEventListener("change", ddlselect);
setCampaign();
}