0

what I am trying to do is generate an html list based from another list on the page. I have a list along the lines of below:

<ul>
  <li class="chosen">content</li>
  <li class="something">content</li>
  <li class="something">content</li>
  <li class="something">content</li>
  <li class="something">content</li>
  <li class="chosen">content</li>
  <li class="chosen">content</li>
</ul>

Whats the best plan of action to create a jquery function which will find the list items with the class of "chosen" and create another list with just these.

Any help would be great. Thank you.

1
  • Are you looking to clone your list, or just add an empty list under the chosen class list items? Commented Jan 29, 2012 at 22:13

1 Answer 1

1

Select all the li elements with class chosen, clone and append into a new ul and finally append the ul to a container whereever you want or page body element.

$('<ul />').append($('ul li.chosen').clone()).appendTo(document.body);

Demo

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

4 Comments

Probably mean <ul/> there, not <u/>. I would edit this but it won't let me because the character difference is less than 6.
Great thanks a lot! just what I wanted. Ill accept once it allows me.
Sorry would this be the same with cloning a table with all the table rows with the class "chosen" as well?
Yes, you can use it on table too.

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.