0

I'm working with "sortable" jQuery elements and trying to re-order them from the application. Somewhat following this example

What I've done is:

1) Create a list of sortable items. Everything works fine

2) Add all the items to this ListItem variable and delete the original list

    var ListItems = $('#lstSortablePSU').find('li');
    $('#lstSortablePSU').empty();

3) Now in a loop I need to look up 'li' elements with specific id's in the ListItems variable and add them back to the sortable list container. Something like:

$('#lstSortablePSU').append(ListItems['#li' + PSUname]);

But I guess this /\ looks up by index not key of the array.

FYI: My ListItems variable looks like this after step 2

enter image description here

6
  • To clarify and simplify: Given the "ListItems" variable above, how do I retrieve an element by matching the ID? So for example I need to get element #liDVDD33, so the result should be same as doing ListItems[5] Commented Mar 13, 2015 at 16:01
  • If you want to get the element with ID liDVDD33, why don't you do $('#liDVDD33')? Commented Mar 13, 2015 at 16:32
  • because I'm not trying to get this element from the DOM, but rather from an array "ListItems" Commented Mar 13, 2015 at 17:49
  • but why? It's entirely unclear what effect you want to achieve. Don't tell me how you want to do it before you told me what you want to do in the first place. compare XY-problem. Commented Mar 13, 2015 at 17:53
  • I have a list of HTML items "li"s that are in some order within a container. I need to re-order these items within a container based on another list. So, what I do, is save all the items into an array "ListItems", remove them from the HTML container, and then add them back in, one at a time in the necessary order. Commented Mar 13, 2015 at 18:11

1 Answer 1

1

As I understand it, you want to toggle the visibility of list items based on whether their IDs are in an array of reference values.

Well, let's do exactly that.

var refValues = ['various', 'ids'];

$('#lstSortablePSU li').toggle(function () {
    var refId = this.id.replace(/^li/, '');

    return $.inArray(refValues, refId) > -1;
});

Note that .toggle() accepts true or false to show or hide elements, but it also accepts a function that calculates that value.

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

Comments

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.