Search code examples
javascriptjqueryjquery-uiselectable

Jquery - returning value from an array


Jfiddle containing my full attempt: http://jsfiddle.net/MdWDP/

As you can see I arranged some text/numbers in an array.

I want to use that as the return value instead of "index+1"

$("#selectable").selectable({

    selected: function (event, ui) {
        $(ui.selected).addClass("ui-selected").siblings().removeClass("ui-selected");
        var labelArr = new Array("Name?", "Date?", "Else", "5", "10", "15", "20", "25", "50", "75", "100");
        var result = $("#select-result").empty();
        $(".ui-selected", this).each(function () {
            var index = $("#selectable li").index(this);
            result.append(index + 1);
            $("#description-name").val(labelArr[ui.value]);
            $("#test").val(index + 1);
        });
    }
});

Solution

  • Your approach at this line is wrong:

    $("#description-name").val(labelArr[ui.value]);
    

    It should be

    $("#description-name").html(labelArr[index]);
    

    <span> element doesn't have $.fn.val method in jQuery. Documentation says:
    The .val() method is primarily used to get the values of form elements such as input, select and textarea

    Also, ui.value in your code is undefined.

    So, here's your updated fiddle.