Back

Pull a dropdown list (select) of other content into a custom field

Description

Use this code in a custom field, to pull a list of other content into a select field. This code uses the jQuery Javascript library in a way that shouldn't interfere with the other Javascript (Dojo) used in the dotCMS back end.

When an item is selected from the list, its value will automatically be copied into the hidden field used by the custom field.

Later, when the content is opened for editing, the hidden field's value will be used to change what is shown as selected in the list.

NOTE: Change anything that says "selectAuthor" to the name of your own custom field.

Code

## Load jQuery if it hasn't already been loaded in this page (ie. by another custom field)
## Start web address with "//" so it will still work when using a secure connection (HTTPS)
<script>window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">\x3C/script>')</script>
<script type="text/javascript">
       
        ${esc.d}.noConflict();
        jQuery(document).ready(function($) {
       
                /* When page is loaded, copy the hidden field value to the dropdown */
                var initialValue = $("#selectAuthor").val();           
                $("#selectAuthor_field select").val(initialValue);
       
                /* When new option is selected, copy dropdown value to the hidden field */
                $("#selectAuthor_field select").change(function(){
                        $("#selectAuthor").val($(this).val());
                });
    });
       
</script>
 
 
<select>
        <option value="">--</option>
        #foreach($author in $dotcontent.pull("+structureName:EbiblestudyAuthor",0,"EbiblestudyAuthor.firstName"))
       
                #set($authorName = "$!author.firstName $!author.lastName")
                #set($authorName = $authorName.toLowerCase().replaceAll("[^a-z]",""))
       
                <option value="$!authorName">$!author.firstName $!author.lastName</option>
               
                #set($authorName = "")
       
        #end
</select>