Attach menu to input

Right-Click on any input
</> Source
<!DOCTYPE html>
<html>
<head>
	<title>Attach menu to input</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/fonts/font_roboto/roboto.css"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/dhtmlx.css"/>
	<script src="../../../codebase/dhtmlx.js"></script>
	<style>
		/* for menu proper showing */
		.dhxform_obj_material div.dhxform_item_label_left div.dhxform_control {
			position: relative;
		}
	</style>
	<script>
		var myForm, formData, menu;
		function doOnLoad() {
			// init form
			formData = [
				{type: "settings", position: "label-left", labelWidth: 110, inputWidth: 120},
				{type: "input", label: "Font Family", name: "font", value: ""},
				{type: "input", label: "Font Size", name: "size", value: ""},
				{type: "input", label: "Font Color", name: "color", value: ""}
			];
			myForm = new dhtmlXForm("myForm", formData);
			
			// init menu
			menu = new dhtmlXMenuObject();
			menu.renderAsContextMenu();
			menu.attachEvent("onBeforeContextMenu", function(zoneId){
				return loadMenuItems(menuZones[zoneId]);
			});
			menu.attachEvent("onClick", function(id, zoneId){
				myForm.setItemValue(menuZones[zoneId], menu.getItemText(id));
			});
			
			// attach menu to form's inputs
			// inputs have random ids
			var t = ["font","size","color"];
			for (var q=0; q<t.length; q++) {
				var id = myForm.getInput(t[q]).id;
				menuZones[id] = t[q];
				menu.addContextZone(id);
			}
		}
		// here ids will stored, after form init values will updated
		// will used in loadMenuItems()
		var menuZones = {};
		var menuItems = {
			font: [
				{id: "tahoma", text: "Tahoma", icon: null},
				{id: "arial", text: "Arial", icon: null},
				{id: "verdana", text: "Verdana", icon: null}
			],
			size: [
				{id: "11", text: "11px", icon: null},
				{id: "12", text: "12px", icon: null},
				{id: "13", text: "13px", icon: null}
			],
			color: [
				{id: "red", text: "Red", icon: null},
				{id: "green", text: "Green", icon: null},
				{id: "blue", text: "Blue", icon: null}
			]
		};
		// will generate items based on clicked item
		function loadMenuItems(id) {
			menu.clearAll();
			for (var q=0; q<menuItems[id].length; q++) {
				menu.addNewChild(menu.topId, q, menuItems[id][q].id, menuItems[id][q].text, false, menuItems[id][q].icon);
			}
			return true;
		}
	</script>
</head>
<body onload="doOnLoad();">
	<div style="padding-bottom: 10px;">Right-Click on any input</div>
	<div id="myForm" style="height:500px;"></div>
</body>
</html>

Documentation

Check documentation to learn how to use the components and easily implement them in your applications.