I want to make a select menu with icons and the text you select should be shown in a text on top (or the icon). My problem is that the on change method is not working.
What's going wrong?
$(function() {
$.widget("custom.iconselectmenu", $.ui.selectmenu, {
_renderItem: function(ul, item) {
var li = $("<li>"),
wrapper = $("<div>", {
text: item.label
});
if (item.disabled) {
li.addClass("ui-state-disabled");
}
$("<span>", {
style: item.element.attr("data-style"),
"class": "ui-icon " + item.element.attr("data-class")
}).appendTo(wrapper);
return li.append(wrapper).appendTo(ul);
},
select: function(event, ui) {
console.log("test")
}
});
$("#icons_id")
.iconselectmenu()
.iconselectmenu("menuWidget")
.addClass("ui-menu-icons avatar");
});
var select = document.getElementById("icons_id");
var options = ["Aufgabe", "Gruppe", "Schritt", "Werkzeug", "Dokument"];
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
var icon = opt;
el.style
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
function myFunction() {
var x = document.getElementById("icons_id").value;
document.getElementById("demo_text").innerHTML = "You selected: " + x;
}
option.avatar {
background-repeat: no-repeat !important;
padding-left: 20px;
}
.avatar .ui-icon {
background-position: left top;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<h2 id="test">Dropdown Menu</h2>
<p id="demo_text">You selected: </p>
<select name="icons_name" id="icons_id" onchange="myFunction()">
<option data-class="avatar" data-style="background-image: url('http://www.gravatar.com/avatar/b3e04a46e85ad3e165d66f5d927eb609?d=monsterid&r=g&s=16');">1</option>
<option data-class="avatar" data-style="background-image: url('http://www.gravatar.com/avatar/e42b1e5c7cfd2be0933e696e292a4d5f?d=monsterid&r=g&s=16');">2</option>
<option data-class="avatar" data-style="background-image: url('http://www.gravatar.com/avatar/bdeaec11dd663f26fa58ced0eb7facc8?d=monsterid&r=g&s=16');">3</option>
</select>
I think the problem that you're having is that you are adding a select method to your widget not setting a select method on the base widget (selectmenu) to use as an event handler.
The following should work: