1

How do I get selected item from a JqxListBox? When I click on list item it should alert me selected item but instead it returns undefined. thx!

my code:

function onS() {
    var listString = new Array();
    var i = 0;
    var listEnumerator = list.getEnumerator();

    while (listEnumerator.moveNext()) {

        var currentItem = listEnumerator.get_current();
        listString[i] = "<br/>" + currentItem.get_title();
        i++;

    }
    $("#jqxWidget").jqxListBox({ source: listString, width: '300px', height: '400px' });

} 
$(document).ready(function () {
    $('#jqxWidget').click( function() {

        var selectedItem = $("#jqxWidget option:selected").last();
        alert("SelectedItem Text: " + selectedItem.html());
    });

});
1

Based on the documentation, It should be

var selectedIndex = $('#jqxWidget').jqxListBox('selectedIndex'); 

For multi-select

// get all items.
var items = $("#jqxWidget").jqxListBox('getItems');
// get selected indexes.
var selectedIndexes = $("#jqxWidget").jqxListBox('selectedIndexes');
var selectedItems = [];
// get selected items.
for (var index in selectedIndexes) {
    if (selectedIndexes[index] != -1) {
        selectedItems[index] = items[index];
    }
}
| improve this answer | |
  • thx but this is returning index not text. I want it to return text. – G.D Nov 24 '15 at 14:16
  • use var item = $("#jqxListBox").jqxListBox('getItem', 1 ); and also read the documentation. 1 is the item index. – Atish Dipongkor - MVP Nov 24 '15 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.