Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate an HTML document with headings into sections

How can I separate in sections a document with headings?

Convert this

<h1>chapter 1</h1>
<p>lorem ipsum</p>
<p>sit amet</p>
<h1>chapter 2</h1>
<p>lorem ipsum</p>
<p>sit amet</p>

into this

<div class="chapter">
  <h1>chapter 1</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>
<div class="chapter">
  <h1>chapter 2</h1>
  <p>lorem ipsum</p>
  <p>sit amet</p>
</div>

I guess with jQuery this is easy, but I haven't figured how yet.

like image 469
Victor Avatar asked May 22 '11 22:05

Victor


People also ask

How do you make separate sections in HTML?

The div tag defines a section or division within a HTML file. It typically contains headings, paragraphs, tables or other elements that need to be grouped together. Commonly used with css by setting the <div class="?"> attribute to set the look and feel of a section of your web page.

How do I split a header into two parts in HTML?

Just move #header-middle last in your HTML. Then what is float: right will go right, float: left will go left, and the middle content will fill upwards and occupy the unclaimed middle space. What is happening the way you have it, is the unfloated element is pushing the floated element after it.

Which tag is used to separate documents into sections?

The <section> tag defines a section in a document.

Can you nest sections in HTML?

Sectioning elements can be nested inside one another as many times as is needed based on the content. The header and footer in a sectioning element can also contain sectioning elements.


2 Answers

Try something like this:

$('h1').each( function(){
    $(this).nextUntil('h1').andSelf().wrapAll('<div class="chapter">');
});

Example: http://jsfiddle.net/redler/YVF2w/

like image 175
Ken Redler Avatar answered Oct 27 '22 00:10

Ken Redler


I guess with jQuery this is easy, but I haven't figured how yet.

Don't do this with jQuery. Write this into your HTML, or use whatever view / template engine your using to write your HTML like this.

Also there is such a thing as <section> and <header> I would recommend using them (but since they are HTML5 you might need html5 shim).

like image 5
Raynos Avatar answered Oct 27 '22 00:10

Raynos