mtin -nong
mtin -nong

Reputation: 53

JQuery, strip all tags, wrap new ones and clone and append

How to clone the p elements in .someClass, remove the p tags while leaving the text, then wrap them with and li tags and finally put those into the .anotherClass's ul tag?

For example:

    //untouched code like this
    <div class="someClass">
    <p>Some text1</p>
    <p>Some text2</p>
    <p>Some text3</p>
    </div>

    //jQuery cloned and apppended code must like this

    <div class="anotherClass">
    <ul>
    <li>Some text1</li>
    <li>Some text2</li>
    <li>Some text3</li>
    </ul>
    </div>

Upvotes: 1

Views: 68

Answers (1)

Macainian
Macainian

Reputation: 362

First of all I would not use classes to identify in this case. I would use an id since you could have more than one "someClass" and more than one "anotherClass", but since I don't know what else you are doing I will answer with the assumption that there is only one of each:

var some_class_div = $(".someClass");
var another_class_ul = $(".anotherClass").find("ul");

some_class_div.find("p").each(function()
{
    another_class_ul.append("<li>" + $(this).text() + "</li>");
});

Might I also recommend that you give the ul an id. Then you can use $("#ul_id"); instead of $(".anotherClass").find("ul");

Upvotes: 1

Related Questions