0

I want to remove outer HTML element from a code snippet to get the content inside including other html elements.

that is,

consider the below code

<p> My Paragraph 
   <ul>Mylist
     <li>Item1</li>
     <li>Item2</li>
   </ul>
</p>

I want to get the output displayed as below

My Paragraph 
       <ul>Mylist
           <li>Item1</li>
           <li>Item2</li>
       </ul>

Is this possible ??

3 Answers3

1

You need to use .unwrap() along with .contents() to target all the contents in it:

 $('p').contents().unwrap(); 

Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

ul elements are not legally allowed inside `p' elements.

you can unwrap() function to do this Refrence

A.B
  • 20,110
  • 3
  • 37
  • 71
0

In your code ul inside p will not work as expected when using

$('p').contents().unwrap();

As suggest by A.B. Use div instead of p, than try

$('div').contents().unwrap(); 
Shyam
  • 782
  • 5
  • 12