tags:

views:

65

answers:

2

Hi, Let's say I have this

$(document).ready(function() {
   var array = $.makeArray($('p'));
   $(array).appendTo(document.body);
   });
});

<p>how</p>
<p>are</p>
<p>you</p>
<p>baby?</p>

If I want to replace <p> with <li> and expected output is ...

<li>how</li>
<li>are</li>
<li>you</li>
<li>baby?</li>

What should I do? Thanks in advance!

+8  A: 
$("p").each(function () {
     $(this).replaceWith("<li>" + $(this).html() + "</li>");
});
Checksum
+1  A: 

Here is a quick and dirty solution, but if you give me more details as far as what you are trying to do we might be able to come up with a better one.

$('p').each(function(){$(this).replaceWith('<li>'+$(this).html()+'</li>')})
Fadi Chalfoun
`$(this).text()` removes the inner html from the `<p>` tags. `.html()` is better
Sander Rijken
Yes that's right Sander Rijken thanks for the catch.
Fadi Chalfoun