Skip to content

Instantly share code, notes, and snippets.

@nicobytes
Last active January 5, 2026 19:55
Show Gist options
  • Select an option

  • Save nicobytes/b9207658e33ab6012d99ae314f62b24d to your computer and use it in GitHub Desktop.

Select an option

Save nicobytes/b9207658e33ab6012d99ae314f62b24d to your computer and use it in GitHub Desktop.
Url Custom Field
#set($titleVarName="titolo")
#set($urlVarName="urltitle")
<script>
function updateDisplayURLTitle(titleValue, urlTitleFieldName, displayElement){
// get the title entered by the user
if(!titleValue || titleValue.length == 0){
displayElement.innerHTML = "";
DotCustomFieldApi.set(urlTitleFieldName, "");
return;
}
var urlTitle = titleValue.toLowerCase();
urlTitle= urlTitle.replace(/^\s+|\s+$/g,"");
urlTitle = urlTitle.replace(/[^a-zA-Z 0-9]+/g,' ');
urlTitle = urlTitle.replace(/\s/g, "-");
// Replace multiple consecutive dashes with a single dash
urlTitle = urlTitle.replace(/-{2,}/g, '-');
// Remove trailing dashes
urlTitle = urlTitle.replace(/-+$/, '');
// Remove leading dashes
urlTitle = urlTitle.replace(/^-+/, '');
displayElement.innerHTML = urlTitle;
DotCustomFieldApi.set(urlTitleFieldName, urlTitle);
}
DotCustomFieldApi.ready(() => {
const titleFieldName = '$titleVarName';
const urlTitleFieldName = '$urlVarName';
const displayElement = document.getElementById("displayURLTitle");
// Initial update
const initialTitleValue = DotCustomFieldApi.get(titleFieldName) || '';
updateDisplayURLTitle(initialTitleValue, urlTitleFieldName, displayElement);
// Watch for changes
DotCustomFieldApi.onChangeField(titleFieldName, value => {
updateDisplayURLTitle(value || '', urlTitleFieldName, displayElement);
});
});
</script>
<div id="displayURLTitle" style="height:20px"> </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment