Skip to content

Instantly share code, notes, and snippets.

@lennardtastic
Last active September 14, 2017 12:28
Show Gist options
  • Save lennardtastic/3474f5d2c76ba7e4776bec073f757810 to your computer and use it in GitHub Desktop.
Save lennardtastic/3474f5d2c76ba7e4776bec073f757810 to your computer and use it in GitHub Desktop.
/**
* Function for adding Custom Variables for Usabilla In-Page widget based on the textContent from an Element based on a classname.
* Use this function after the function usblInPageAppendToClassMulti and before the loading of the in-page install script.
*
* @param {String} classnamedata [The classname of which the textcontent will retrieved]
* @param {String} propertyname [The property name that should be used for the custom variables]
*/
function usblInPageAddCustomVarMulti(classnamedata, propertyname){
var inPageWidgetElementList = document.querySelectorAll("div[ub-in-page]");
var faqToggleElementList = document.getElementsByClassName(classnamedata);
var arrayLengthInpage = inPageWidgetElementList.length;
var arrayLengthFaqToggle = faqToggleElementList.length;
var customVarValue;
var inPageWidgetElementNode;
for (var i = 0; i < arrayLengthInpage; i++) {
inPageWidgetElementNode = inPageWidgetElementList[i];
customVarValue = faqToggleElementList[i].textContent.trim();
var usblInPageCustom = document.createAttribute("ub-in-page-" + propertyname);
usblInPageCustom.value = customVarValue;
inPageWidgetElementNode.setAttributeNode(usblInPageCustom);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment