I am trying to add and remove listbox items via modifying the select tag it is based off of when initilizing. So when a user presses a button it will add options tags to the select and when the user presses another button it should refresh the listbox with the new items added by the user. For some reason I cannot get it to work. I've tried destroy() and then remaking the listboxes but that only gives me an empty listbox with no controls. Any help would be appreciated thank you
Here is my code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://kendo.cdn.telerik.com/2020.1.114/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
<base href="https://demos.telerik.com/kendo-ui/listbox/index?_ga=2.84834905.1992168603.1580165777-1170392125.1577463742">
<title></title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
</head>
<body>
<form id="form1" runat="server">
<div runat="server" id="example" role="application" >
<select runat="server" id="size" style="width: 100%;" ></select><br><br>
<div class="demo-section k-content">
<div>
<label for="optional" id="employees">Allowed Roles</label>
<label for="selected">Disallowed Roles</label>
<br />
<select id="optional" runat="server">
<option value="min">Text1</option>
<option value="Adn">Text Fo</option>
<option value="Adm">Text #</option>
<option value="Ain">Text 5</option>
</select>
<select id="selected" runat="server">
<option value="min">Text Six</option>
<option value="Amin">Text Sev</option>
<option value="Adn">Text Eght</option>
<option value="AT">Text Twentt</option>
</select>
</div>
<input type="button" id="submit" value="submt" class=""/>
<input type="button" id="Button1" value="yoooo"/>
</div>
</div>
<style>
html {
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
}
#panelbar {
width: 600px;
max-width: 700px;
margin: 0 auto;
}
.demo-section label {
margin-bottom: 5px;
font-weight: bold;
display: inline-block;
}
#employees {
width: 270px;
}
#example .demo-section {
max-width: none;
width: 600px;
}
#example .k-listbox {
width: 236px;
height: 310px;
}
#example .k-listbox:first-of-type {
width: 270px;
margin-right: 1px;
}
</style>
<script>
$(document).ready(function () {
var template = "<option value='#: Department #'>#: Function #</option>";
$("#submit").click(function () {
var d = $('#size').val();
alert(d); //alert(c);
$('#optional > option').each(function () {
//alert($(this).text() + ' ' + $(this).val());
});
var listBox1 = $("#optional").data("kendoListBox");
// refreshes the list box
listBox1.remove(listBox1.items());
listBox1.destroy();
var listBox = $("#selected").data("kendoListBox");
// refreshes the list box
listBox.remove(listBox.items());
listBox.destroy();
listbox.items.add
alert("destroyed kendo boxes")
$("#optional").kendoListBox({
connectWith: "selected",
toolbar: {
tools: ["transferTo", "transferFrom", "transferAllTo", "transferAllFrom"]
}
});
$("#selected").kendoListBox();
//$("#size").append('<option value="empl@c1.com">option6</option>');
//$("#size").kendoDropDownList({onChange: onChange});
});
$("#Button1").click(function () {
$('#optional').append(`<option value="dios"> carao </option>`);
$('#selected').append(`<option value="dios"> carao </option>`);
});
$("#optional").kendoListBox({
connectWith: "selected",
toolbar: {
tools: ["transferTo", "transferFrom", "transferAllTo", "transferAllFrom"]
}
});
$("#selected").kendoListBox();
$("#size").kendoDropDownList({
change: onChange
});
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
//called when user switches employee name in drop down list
//to do -- remove all current contents and replace with permissions for that user
function onChange() {
//alert("here");
//$("#optional").empty();
// refreshes the list box
//listbox.refresh();
//listBox.dataSource.read();
};
});
</script>
</form>
</body>
</html>