0

All, Say you have a Html string like below. I want to select all the li element from it, and append them to a ul.

var sHtml="<li>..</li><li>..</li><li>..</li><li>..</li><span>xxx</span>";
$("#myUl").append($("li",$(sHtml)));

But this code doesn't work . Is there any way to make it using jquery?

1

3 Answers 3

1

Try this,

var sHtml="<li>..</li><li>..</li><li>..</li><li>..</li><span>xxx</span>";
$(sHtml).not('span').appendTo("#myUl");

Fiddle

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

Comments

1

Try this:

var sHtml="<li>..</li><li>..</li><li>..</li><li>..</li><span>xxx</span>";
var htm = $(sHtml);
$("#myUl").append(htm).find("span").remove();

DEMO

Comments

0

If what you are trying to achieve is overwriting part of your current page with html from maybe a page returned by an AJAX call, then give the <ul> an id and do something like

$("#myliID").html($(returnedPage).find("#myliID").html());

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.