views:

117

answers:

2

how to get 'id' of item in selectable list, if the list is created dynamically?

  <ul id="selectable">
 <li id='1'>..</li>
    .
    .
    <li...
  </ul>

I tried var num = $('#selecable :selected').attr( "option" , 'id' );

but get only [object Object]... what is the right way?

+1  A: 

The list of selected id's would be: edit: better methinnks

    $('#selectable").selectable(function(){
      selected:function(){ 
                                var idlist = $(this).attr('id');
   //do something with the list...of id's
                        }
    });
Mark Schultheiss
`:selected` does not work on `li` elements.
Felix Kling
yea, I had a brain cramp, edited the silly thing :) while you were commenting.
Mark Schultheiss
+2  A: 

Update:

For completeness, if an element is selected, the plugin adds a class ui-selected to the element. So you can get the ID of current selected element via:

$('#selectable .ui-selected').attr('id');

But be aware that multiple elements can be selected.


The jQuery UI selectable plugin calls a callback whenever you select an element, you just have to provide it:

$("#selectable" ).selectable({
   selected: function(event, ui) { ... }
});

That said, as Nick already mentions, IDs cannot start with a digit.

:selected only works on option elements.

Felix Kling