tags:

views:

34

answers:

2

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>
+2  A: 

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");
});
cletus
thxs but I'm not seeing how that would work with the previous? $("<li>").text($(this).text()).appendTo("#theList")
AnApprentice
The third one did it, thanks cletus
AnApprentice
@nobosh: see the updated version. It works instead of the `each()` loop.
cletus
A: 

you can probably use wrap:

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