0

I would like to sort several ULs with jquery. The criteria is that the LI containing the text "current" must be moved to the top of that list.

example

<ul>
    <li>doc1</li>
    <li>doc5</li>
    <li>doc3</li>
    <li>doc54 (current)</li>
    <li>doc1</li>
</ul>

would become

<ul>
    <li>doc54 (current)</li>
    <li>doc1</li>
    <li>doc5</li>
    <li>doc3</li>
    <li>doc1</li>
</ul>

(but there are many of these lists on the page, all different and not always containing an item which is labeled "current" which all must be sorted)

Help would be appreciated. JSFiddles even more appreciated.

1 Answer 1

4

Try something like:

$('li:contains("current")').each(function () {
  $(this).insertBefore(
    $(this).parent().children().not(this).eq(0)
  );
});

http://jsfiddle.net/D2EkM/4/

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

2 Comments

slight bug in your code. - if there is only one entry with the word "current" it deletes it out of the list!
well done, your update also includes cases where it is first in list, not just only in list like my edit did.

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.