DHTMLX Docs & Samples Explorer

Menu Items Images Manipulation

Select Item  
(Click menu item to select)
Current Image  
Select New Image  
Remove Item Image  

Source
<link rel="stylesheet" type="text/css" href="../../codebase/skins/dhtmlxmenu_dhx_skyblue.css">
<script src="../../codebase/dhtmlxcommon.js"></script>
<script src="../../codebase/dhtmlxmenu.js"></script>
<script src="../../codebase/ext/dhtmlxmenu_ext.js"></script>
 
<div style="height: 250px;"><div id="menuObj"></div></div>
<div>
    <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>Select Item</td>
            <td width="10">&nbsp;</td>
            <td><div id="item">(Click menu item to select)</div></td>
        </tr>
        <tr>
            <td>Current Image</td>
            <td>&nbsp;</td>
            <td><img id="imgcur" src="" width="18" height="18" style="border: #909090 1px solid;"></td>
        </tr>
        <tr>
            <td>Select New Image</td>
            <td>&nbsp;</td>
            <td id="imgList"></td>
        </tr>
        <tr>
            <td>Remove Item Image</td>
            <td>&nbsp;</td>
            <td><input type="button" value="Remove Image" onclick="update();"></td>
        </tr>
    </table>
</div>
 
<br>
<script>
var menu,
mid;
function initMenu() {
    menu = new dhtmlXMenuObject("menuObj");
    menu.setIconsPath("../common/imgs/");
    menu.attachEvent("onClick", menuClick);
    menu.loadXML("../common/dhxmenu.xml?e=" + new Date().getTime(), function() {
        menu.forEachItem(function(id) {
            var imgs = menu.getItemImage(id);
            if (imgs[0] != null && imgs[0] != "") {
                var text = '<span><img onclick="changeIcon(this, \'' + imgs[0] + '\');" width="18" height="18" border="0" src="../common/imgs/' + imgs[0] + '"></span> ';
                document.getElementById("imgList").innerHTML += text;
            }
        });
    });
}
function menuClick(id) {
    var img = menu.getItemImage(id);
    document.getElementById("item").innerHTML = id + " (" + menu.getItemText(id) + ")";
    document.getElementById("imgcur").src = "../common/imgs/" + img[0];
    //.replace(/^\.\.\//,"");
    mid = id;
}
function changeIcon(obj, icon) {
    if (mid == null) {
        return;
    }
    document.getElementById("imgcur").src = obj.src;
    menu.setItemImage(mid, icon);
}
function update() {
    menu.clearItemImage(mid);
    document.getElementById("imgcur").src = "../common/imgs/";
}
</script>