3

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!

3
  • 2
    you probably also want an <ul> around that? Commented Mar 30, 2010 at 8:05
  • 1
    No I don't. Just need simple one and I've got answer :) Commented Mar 30, 2010 at 8:36
  • 1
    just a bunch of <li> elements is invalid html/xhtml. The "works for me" scenario might hurt in the future Commented Mar 30, 2010 at 21:16

2 Answers 2

9
$("p").each(function () {
     $(this).replaceWith("<li>" + $(this).html() + "</li>");
});
Sign up to request clarification or add additional context in comments.

Comments

1

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>')})

1 Comment

$(this).text() removes the inner html from the <p> tags. .html() is better

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.