AnApprentice
AnApprentice

Reputation: 110960

JQUERY, TEXT, appending an LI to a list with a SPAN wrapped?

here is my current line of code:

$("<li>").text($(this).text()).appendTo("#theList");

Which results in

<ul id="theList">
<li>blah</li>
<li>blah</li>
</ul>

How can I update that code above to result in:

<ul id="theList">
<li><span>blah</span></li>
<li><span>blah</span></li>
</ul>

Upvotes: 0

Views: 1374

Answers (2)

cletus
cletus

Reputation: 625097

There are two ways of doing this. I know from your previous question you are getting this data from a span in which case you can clone() it:

$("span.findme").clone().appendTo("#theList");

You might want to remove the class:

$("span.findme").clone().removeClass("findme").appendTo("#theList");

Alternatively using each():

$("span.findme").each(function() {
  var span = $("<span>").text($(this).text());
  $("<li>").append(span).appendTo("#theList");
});

Upvotes: 2

adharris
adharris

Reputation: 3661

you can probably use wrap:

$("<li>").text($(this).text()).wrap("<span></span>").appendTo("#theList");

Upvotes: 0

Related Questions