Getting Started with JavaScript DropDownList

14 Nov 20237 minutes to read

The external script dependencies of the DropDownList widget are,

And the internal script dependencies of the DropDownList widget are:

File  Description / Usage
ej.core.min.js Must be referred always before using all the JS controls.
ej.data.min.js Used to handle data operation and should be used while binding data to JS controls.
ej.dropdownlist.min.js The dropdownlist’s main file
ej.checkbox.min.js Should be referred when using checkbox functionalities in DropDownList.
ej.scroller.min.js Should be referred when using scrolling in DropDownList.
ej.draggable.min.js Should be referred when using popup resize functionality in DropDownList.

For getting started you can use the ‘ej.web.all.min.js’ file, which encapsulates all the ‘ej’ controls and frameworks in one single file.

For themes, you can use the ‘ej.web.all.min.css’ CDN link from the snippet given. To add the themes in your application, please refer this link.

Preparing HTML document

Create a new HTML file and add CDN links to the JavaScript and CSS dependencies to your project.

  • HTML
  • <!DOCTYPE html>
    
        <html>
    
        <head>
    
            <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8" />
    
            <!-- style sheet for default theme(flat azure) -->
    
            <link href="http://cdn.syncfusion.com/24.2.3/js/web/flat-azure/ej.web.all.min.css"
                  rel="stylesheet" />
    
            <!--scripts-->
    
            <script src="http://cdn.syncfusion.com/js/assets/external/jquery-1.11.3.min.js"></script>
    
            <script src="http://cdn.syncfusion.com/js/assets/external/jquery.easing.1.3.min.js"></script>
    
            <script src="http://cdn.syncfusion.com/24.2.3/js/web/ej.web.all.min.js"></script>
    
        </head>
    
        <body>
    
            <!--Place input element to create DropDownList-->
    
            <script>
    
                // Place your script code here to initialize DropDownList
    
            </script>
    
        </body>
    
        </html>

    NOTE

    In production, we highly recommend you to use our custom script generator to create custom script file with required controls and its dependencies only. Also to reduce the file size further please use GZip compression in your server.

    Creating DropDownList

    The DropDownList can be created from a HTML ‘select’ element with the HTML ‘id’ attribute and pre-defined options set to it. To create the DropDownList, you should call the ‘ejDropDownList’ jQuery plug-in function.

  • HTML
  • <select id="dropdown1">
    	
    		<option value="item1">ListItem 1</option>
    			
    		<option value="item2">ListItem 2</option>
    					
    		<option value="item3">ListItem 3</option>
    				
    		<option value="item4">ListItem 4</option>
    				
    		<option value="item5">ListItem 5</option>
    				
    	</select>
  • JAVASCRIPT
  • $(function () {
    				
    			$('#dropdown1').ejDropDownList();
    					
    		});

    Getting_Started_Image1

    Populating data

    The DropDownList can be bounded to both local array and remote data services using ej.DataManager. You can use DataManager component to serve data from the data services based on the query provided. To bind data to DropDownList widget, the dataSource property should be assigned with the instance of ‘ej.DataManager’.

    NOTE

    ODataAdaptor is the default adaptor for DataManager. On binding to other web services, proper data adaptor needs to be set on ‘adaptor’ option of DataManager.

  • HTML
  • <input type="text" id="dropdown1" />
  • JAVASCRIPT
  • $(function () {
                // declaration
                Customers = [
                     { id: "1", text: "ALFKI" }, { id: "2", text: "ANATR" }, { id: "3", text: "ANTON" },
                     { id: "4", text: "AROUT" }, { id: "5", text: "BERGS" }, { id: "6", text: "BLAUS" }
                ];
                $('#dropdown1').ejDropDownList({
                    dataSource: ej.DataManager(Customers),
                    fields: { id: "id", text: "text", value: "text" }
                });
            });
    	});

    Getting_Started_Image2

    Setting Dimensions

    DropDownList dimensions can be set using width and height API.

  • HTML
  • <input type="text" id="dropdown1" />
  • JAVASCRIPT
  • $(function () {
    			
    		$('#dropdown1').ejDropDownList({
    			
    			width: 300,
    			
    			height: 50
    		});
    	});

    Setting dimensions to Popup list

    PopupWidth and popupHeight can be used to create a fixed size popup list.

  • HTML
  • <input type="text" id="dropdown1" />
  • JAVASCRIPT
  • $(function () {
    	
    		// declaration
    		
    		var items = [{ text: "ListItem 1", value: "item1" },
    		
    			{ text: "ListItem 2", value: "item2" },
    					
    			{ text: "ListItem 3", value: "item3" },
    					
    			{ text: "ListItem 4", value: "item4" },
    				
    			{ text: "ListItem 5", value: "item5" }];
    					
    		$('#dropdown1').ejDropDownList({
    		
    			dataSource: items,
    			
    			fields: { text: "text", value: "value" },
    			
    			width: 500,
    			
    			height: 50,
    			
    			popupHeight: 200,
    			
    			popupWidth: 300
    			
    		});
    		
    	});

    Setting and Getting Value

    You can select single or multiple values from DropDownList widget. To assign a value initially to the DropDownList, you can use value property.

    NOTE

    To select multiple items based on index, refer here.

  • HTML
  • <input type="text" id="dropdown1" />
  • JAVASCRIPT
  • $(function () {
    		
    		var items = [{ text: "ListItem 1", value: "item1" },
    		
    			{ text: "ListItem 2", value: "item2" },
    			
    			{ text: "ListItem 3", value: "item3" },
    				
    			{ text: "ListItem 4", value: "item4" },
    				
    			{ text: "ListItem 5", value: "item5" }];
    				
    				
    		$('#dropdown1').ejDropDownList({
    					
    			dataSource: items,
    				
    			fields: { text: "text", value: "value" },
    				
    			value: "item3"
    		});
    			
    		var obj = $('#dropdown1').data("ejDropDownList");
    				
    		console.log("Selected Item's Text - " + obj.option("text"));
    				
    		console.log("selected Item's Value - " + obj.option("value"));       
    				
    	});