Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:51:34+00:00 2026-05-27T09:51:34+00:00

I have a block of HTML with nested headings, paragraphs etc. Is there a

  • 0

I have a block of HTML with nested headings, paragraphs etc.

Is there a jQuery/JavaScript plugin/function/snipet that can unnest them?, or how would I go about doing it?

I can supply a list of tags that should not be nested.

For example I have

<h1>
    Heading 1
    <h2>
        Heading 2
        <h3>
            Heading 3
        </h3>
    </h2>
</h1>

<p>
    Paragraph
    <p>
        Paragraph
    </p>
    <p>
        Paragraph
    </p>
</p>

<p>
    Paragraph
    <h1>
        Heading 1
        <h6>
            Heading 6
        </h6>
        Heading 1
    </h1>
    Paragraph
<p>

Which should respectively be converted to

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>

<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>

<p>Paragraph<p>
<h1>Heading 1</h1>
<h6>Heading 6</h6>
<h1>Heading 1</h1>
<p>Paragraph<p>

Update:

I have noticed the Firefox and Chrome at least does some automatic correction when the HTML is appended to the DOM. (as I have the HTML in a string, but can append it to he DOM if needed)

The only thing it does not do is remove p tags out of headings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:51:34+00:00Added an answer on May 27, 2026 at 9:51 am

    Most browsers will correct the DOM to be valid.

    So I see now that the browser does do some correction, which is good but the only thing I notice it does not do, is move p‘s out of headings.

    This code will move p elements to after their heading element.

    $('p').filter(function() {
        return $(this).closest(':header').length;
    }).each(function() {
        $(this).insertAfter($(this).closest(':header'));
    });
    

    jsFiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a block of code that serializes a type into a Html tag.
I have a server-generated html like: <ul> <li><!-- few nested elements that form a
I have a block of html code that I don't have control over. I
I have an Android/HTML hybrid application that has a small number of nested ViewFlippers
I have this block of html: <div> <p>First, nested paragraph</p> </div> <p>First, non-nested paragraph.</p>
I have a block of html in a string that is basically a list
I have a nested CSS menu that I can't get the submenus to come
I have a large block of HTML that needs to be replaced, which includes
I have a block of HTML: <ul> <li>a</li> <li>b</li> <li class=nav>c</li> <li class=nav>d</li> </ul>
I have html and css as below - .title { display: block; background-color: red;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.