Comma separate jQuery selectable items
-
04-12-2019 - |
Question
I am using jQuery and the widget Selectable to let my user select files to delete. I am appending each selection to a div to be able to see what divs I have selected. I need to comma seperate the values (ids) to be able to use it when saving.
How can I comma seperate? Is there a better way of doing this?
$("#photo_area").selectable({
cancel: 'a',
stop: function() {
var result = $("#selected").empty();
$(".ui-selected", this).each(function() {
var index = $(this).attr('id');
result.append(index);
});
}
});
Solution
There's nothing blatantly wrong with what you already have, but I think the following is a little more concise:
var ids = $(this).find(".ui-selected").map(function() {
return this.id;
}).get().join(",");
OTHER TIPS
Not sure what you are after. But you can always use a comma speerated string as a jQuery selector.
$('#myId2, #myId1, #myId3')
..fredrik
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow