3

I have a jQuery UI selectable list: http://jqueryui.com/demos/selectable/

each row I generate has a unique ID that looks like:

<ol id="selectable">
  <li class="ui-widget-content" data-userid="5">test</li>
  <li class="ui-widget-content" data-userid="6">adfsg</li>
  <li class="ui-widget-content" data-userid="7">ghj</li>
  <li class="ui-widget-content" data-userid="8">fhjk</li>
  <li class="ui-widget-content" data-userid="9">fhn</li>
</ol>

What I need to do is loop through the list and get the data-userid attribute value for ones that are selected.

My JS so far is just this:

$('#selectable').selectable().bind("selectableselected",function(event,ui)
{

});

which if I do a console.log(ui.selected), I get the whole HTML string. I think I am going about this the wrong way. Any ideas?

2 Answers 2

3

The plugin adds a .ui-selected class for selected items so it's pretty easy to get the selected items.

The .map() function allows to return a (sort-of) array with a callback to return the data:

var ids = $('#selectable .ui-selected').map(function() {
    return $(this).data('userid');
});

// to get a pure javascript array
ids.toArray();

Here's a fiddle to play with.

Sign up to request clarification or add additional context in comments.

1 Comment

I have added some css and a more helpful alert to the fiddle.
2

Assuming the class for a selected item would be ui-selected:

$('#selectable .ui-widget-content.ui-selected').each(function(index) {
     alert($(this).attr('data-userid'));
});

1 Comment

yea that currently gets ALL the userids, but very close!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.