views:

108

answers:

1

For my webpage, I have a number of lists which I'm generating dynamically from the database. Each list is generated like so:

<ul id = "<%= "subgroups_for_tumourgroup_" + item.ID %>">

I'm trying to make them sortable using jquery's sortable list

<script type="text/javascript">

    $(function() {

    $('#subgroups_for_tumourgroup_1').sortable();

    });   
</script> 

The question is, given that I may have any number of IDs (the "1" in the code above is the ID) and they may not even be sequential (I may have lists called "subgroups_for_tumourgroup_1", and "subgroups_for_tumourgroup_3", but no "subgroups_for_tumourgroup_2", how do I make all those lists independantly sortable?

+2  A: 

You can use Attribute Starts With Selector to match all IDs up to the point right before the number that gives them their uniqueness:

// make all ULs whose id starts with 'subgroups_for_tumourgroup_' into sortables
$('ul[id^=subgroups_for_tumourgroup_]').sortable();

That said, can't you just use a class selector instead, and not have to bother with IDs?

$("ul.sortable").sortable();
karim79
Oh wow, that works - I had no idea I could make a .sortable call on a text match pattern rather than the exact name, and I was trying to parse out the ID from the <ul> tag and do string concatenation and looping through all of them. Thanks a bunch.
sslepian